Skip to content

Commit 8b31585

Browse files
committed
feat: [Week 06-4] solve longest-increasing-subsequence
1 parent 9b3dbce commit 8b31585

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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)

0 commit comments

Comments
 (0)