File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
longest-increasing-subsequence Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Solution: 1) DFS Brute Force -> TLE
3
+ Time: O(2^n * nlogn)
4
+ """
5
+
6
+
7
+ class Solution :
8
+ def lengthOfLIS (self , nums : List [int ]) -> int :
9
+
10
+ sub = []
11
+ max_len = 0
12
+
13
+ def dfs (i , length ):
14
+ nonlocal max_len
15
+ if i == len (nums ):
16
+ if sub == sorted (list (set (sub ))):
17
+ max_len = max (len (sub ), max_len )
18
+ return
19
+
20
+ dfs (i + 1 , length )
21
+ sub .append (nums [i ])
22
+ dfs (i + 1 , length )
23
+ sub .pop ()
24
+
25
+ dfs (0 , 0 )
26
+ return max_len
27
+
28
+
29
+ """
30
+ 풀이를 보고 적었으며, 완벽히 이해 되지는 않습니다.
31
+ Time: O(n^2)
32
+ Space: O(n)
33
+ """
34
+
35
+
36
+ class Solution :
37
+ def lengthOfLIS (self , nums : List [int ]) -> int :
38
+ LIS = [1 ] * len (nums )
39
+
40
+ for i in range (len (nums ) - 1 , - 1 , - 1 ):
41
+ for j in range (i + 1 , len (nums )):
42
+ if nums [i ] < nums [j ]:
43
+ LIS [i ] = max (LIS [i ], 1 + LIS [j ])
44
+ return max (LIS )
You can’t perform that action at this time.
0 commit comments