Skip to content

Commit 9f06b6e

Browse files
committed
491
1 parent 1334224 commit 9f06b6e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

DFS/traditionalDFS/491.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## 491 Increasing Subsequences
2+
3+
#### Description
4+
5+
[link](https://leetcode.com/problems/increasing-subsequences/)
6+
7+
---
8+
9+
#### Solution
10+
11+
其实这题的核心在于如何去重,答案提供者很巧妙使用了一个方法,就是Tuple,因为Tuple本身是可以直接去重的,(5,)这种表示形式表示该Tuple只有一个数字,等待添加,记得每次保留空Tuple以添加下一个单数字
12+
13+
**元组相加操作即是按顺序合并两个tuple**
14+
15+
**这里讲一下DFS的时间复杂度判断:如果是最坏情况的话,首先因为初始DFS的循环是O(M + N),每个点的最大遍历长度是O(MN),这就是最坏情况的时间复杂度**
16+
17+
---
18+
19+
#### Code
20+
21+
> 最坏情况下是O(n^2)
22+
23+
```python
24+
class Solution:
25+
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
26+
subs = {()}
27+
for num in nums:
28+
subs |= {sub + (num,)
29+
for sub in subs
30+
if not sub or sub[-1] <= num}
31+
return [sub for sub in subs if len(sub) >= 2]
32+
```

0 commit comments

Comments
 (0)