-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathincreasing_subsequences.py
83 lines (76 loc) · 2.32 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
# V0
# IDEA : DFS
class Solution(object):
def findSubsequences(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = set()
self.dfs(nums, 0, res, [])
return map(list, res)
def dfs(self, nums, index, res, path):
if len(path) >= 2:
res.add(tuple(path))
for i in range(index, len(nums)):
### be aware of it
if not path or nums[i] >= path[-1]:
### be aware of it
self.dfs(nums, i + 1, res, path + [nums[i]])
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79827505
# IDEA : DFS
class Solution(object):
def findSubsequences(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = set()
self.dfs(nums, 0, res, [])
return map(list, res)
def dfs(self, nums, index, res, path):
if len(path) >= 2:
res.add(tuple(path))
for i in range(index, len(nums)):
if not path or nums[i] >= path[-1]:
self.dfs(nums, i + 1, res, path + [nums[i]])
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79827505
# IDEA : DP
class Solution(object):
def findSubsequences(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
dp = set()
for n in nums:
for y in list(dp):
if n >= y[-1]:
dp.add(y + (n,))
dp.add((n,))
return list(e for e in dp if len(e) > 1)
# V1''
# https://www.jiuzhang.com/solution/increasing-subsequences/#tag-highlight-lang-python
class Solution(object):
def findSubsequences(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
self.subsets(nums, 0, [], res)
return res
def subsets(self, nums, index, temp, res):
if len(nums) >= index and len(temp) >= 2:
res.append(temp[:])
used = {}
for i in range(index, len(nums)):
if len(temp) > 0 and temp[-1] > nums[i]: continue
if nums[i] in used: continue
used[nums[i]] = True
temp.append(nums[i])
self.subsets(nums, i+1, temp, res)
temp.pop()
# V2