Skip to content

Commit d9c75c8

Browse files
kamyu104githubutilities
authored andcommitted
Create increasing-subsequences.py
1 parent d6a0b3d commit d9c75c8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Python/increasing-subsequences.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Time: O(n * 2^n)
2+
# Space: O(n^2)
3+
4+
# Given an integer array, your task is
5+
# to find all the different possible increasing
6+
# subsequences of the given array,
7+
# and the length of an increasing subsequence should be at least 2 .
8+
#
9+
# Example:
10+
# Input: [4, 6, 7, 7]
11+
# Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
12+
# Note:
13+
# The length of the given array will not exceed 15.
14+
# The range of integer in the given array is [-100,100].
15+
# The given array may contain duplicates,
16+
# and two equal integers should also be considered as a special case of increasing sequence.
17+
18+
class Solution(object):
19+
def findSubsequences(self, nums):
20+
"""
21+
:type nums: List[int]
22+
:rtype: List[List[int]]
23+
"""
24+
def findSubsequencesHelper(nums, pos, seq, result):
25+
if len(seq) >= 2:
26+
result.append(list(seq))
27+
lookup = set()
28+
for i in xrange(pos, len(nums)):
29+
if (not seq or nums[i] >= seq[-1]) and \
30+
nums[i] not in lookup:
31+
lookup.add(nums[i])
32+
seq.append(nums[i])
33+
findSubsequencesHelper(nums, i+1, seq, result)
34+
seq.pop()
35+
36+
result, seq = [], []
37+
findSubsequencesHelper(nums, 0, seq, result)
38+
return result

0 commit comments

Comments
 (0)