Split Array into Consecutive Subsequences

Problem Id: 659 Difficulty: Medium


Intuition

Solution


class Solution:
    def isPossible(self, nums: List[int]) -> bool:
        e1 = defaultdict(int)
        e2 = defaultdict(int)
        e3 = defaultdict(int)
        for num in nums:
            if e1[num]:
                e1[num] -= 1
                e2[num + 1] += 1
            elif e2[num]:
                e2[num] -= 1
                e3[num + 1] += 1
            elif e3[num]:
                e3[num] -= 1
                e3[num + 1] += 1
            else:
                e1[num + 1] += 1
        return max(e1.values()) == max(e2.values()) == 0