Non recursive python sol


  • 0
    L
    class Solution(object):
        def findSubsequences(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
    
            subSeqSet = set()
            for i,n in enumerate(nums):
                tempSubSeq = {} # priority , entry tuple
    
                for j in range(i+1,len(nums)):
                    subsToAdd = []
                    
                    for sub  in sorted( tempSubSeq.keys() ):
                        if sub > nums[j] :
                            break
                        vals = tempSubSeq[ sub ]
                        for v in vals:
                            newSub = v + (nums[j],)
                            if newSub not in subSeqSet:
                                subsToAdd.append( newSub )
                                subSeqSet.add(newSub)
                                
                    for subToAdd in subsToAdd:
                        tempSubSeq.setdefault(subToAdd[-1] , [] ).append( subToAdd )
    
                    if nums[i] <= nums[j] :
                        subToAdd = (nums[i] , nums[j])
                        if subToAdd not in subSeqSet:
                            tempSubSeq.setdefault(subToAdd[-1], []).append(subToAdd)
                            subSeqSet.add( subToAdd )
    
            return list(subSeqSet)
    

Log in to reply
 

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