Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 655 Bytes

659.md

File metadata and controls

23 lines (21 loc) · 655 Bytes

659. Split Array into Consecutive Subsequences

Solution 1 (time O(n*log(n)), space O(n))

class Solution(object):
    def isPossible(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        heaps = collections.defaultdict(list)
        for num in nums:
            if heaps[num - 1]:
                prev_len = heapq.heappop(heaps[num - 1])
                heapq.heappush(heaps[num], prev_len + 1)
            else:
                heapq.heappush(heaps[num], 1)
        for cur_heap in heaps.values():
            if cur_heap and cur_heap[0] < 3:
                return False
        return True