-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path491 Increasing Subsequences.py
72 lines (62 loc) · 1.9 KB
/
491 Increasing Subsequences.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python3
"""
Given an integer array, your task is to find all the different possible
increasing subsequences of the given array, and the length of an increasing
subsequence should be at least 2 .
Example:
Input: [4, 6, 7, 7]
Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7],
[4,7,7]]
Note:
The length of the given array will not exceed 15.
The range of integer in the given array is [-100,100].
The given array may contain duplicates, and two equal integers should also be
considered as a special case of increasing sequence.
Author: Rajeev Ranjan
"""
class Solution:
def findSubsequences(self, nums):
"""
2nd approach
Maintain the current increasing subsequence and iterate them to grow it
Same complexity as the 1st approach since both needs to iterate the
subsequences
:type nums: List[int]
:rtype: List[List[int]]
"""
subs = set()
for n in nums:
subs |= set([
sub + (n,)
for sub in subs
if n >= sub[-1]
])
subs.add((n,))
return [
list(sub)
for sub in subs
if len(sub) >= 2
]
def findSubsequences(self, nums):
"""
1st approach.
F[i] records the increasing subsequence ends at A[i]
F[i] = F[j] + A[i].
iterating F[j] is exponential
:type nums: List[int]
:rtype: List[List[int]]
"""
l = len(nums)
F = [
[(nums[i],)]
for i in range(l)
]
ret = set()
for i in range(1, l):
for j in range(i):
if nums[i] >= nums[j]:
for t in F[j]:
cur = t + (nums[i],)
ret.add(cur)
F[i].append(cur)
return list(map(list, ret))