Iterative python soluton


  • 0
    I

    Its kind of dumb but... owel lol

    class Solution(object):
        def findSubsequences(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            n = len(nums)
            if n <= 1:
                return []
                
            subseqs = [[[nums[i]]] for i in xrange(n)]
            already_added = set()
            res = []
            
            for i in xrange(1, n):
                for j in xrange(i):
                    if nums[j] <= nums[i]:
                        for seq in subseqs[j]:
                            new_seq = seq + [nums[i]]
                            new_seq_hash = str(new_seq)
                            if len(new_seq) > 1 and new_seq_hash not in already_added:
                                already_added.add(new_seq_hash)
                                res.append(new_seq)
                            subseqs[i].append(new_seq)
                            
            return res

Log in to reply
 

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