Leetcode 1422 Solution

This article provides solution to leetcode question 1422 (divide-array-in-sets-of-k-consecutive-numbers)

https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers

Solution

class Solution:
    def isPossibleDivide(self, nums: List[int], k: int) -> bool:
        m = collections.defaultdict(int)
        for num in nums:
            m[num] += 1

        left = len(nums)

        while left >= k:
            first_key = sorted(m.keys())[0]

            for i in range(k):
                if first_key + i not in m:
                    return False

                m[first_key + i] -= 1
                left -= 1

                if m[first_key + i] == 0:
                    del m[first_key + i]

        return left == 0