Python Simple O(nlogn)


  • 0

    Idea: Always place a number to the list that has the shortest length

    class Solution(object):
        def isPossible(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            d = collections.defaultdict(list)
            for num in nums:
                if d[num - 1]:
                    heapq.heappush(d[num], heapq.heappop(d[num - 1]) + 1)
                else:
                    heapq.heappush(d[num], 1)
            return not any(length < 3 for length in sum(d.values(), []))
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.