Skip to content

Commit 16b7109

Browse files
committed
#272 longest-increasing-subsequence solution
1 parent c934be0 commit 16b7109

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
풀이 :
3+
해당 인덱스의 숫자로 끝나는 LIS의 길이를 dp배열에 저장
4+
nums[i] 와 그 이후에 오는 nums[j]를 비교해 nums[j]가 크고
5+
i까지의 LIS 길이 + 1 > j까지의 LIS길이이면 j까지의 LIS길이 업데이트
6+
업데이트 할 때마다 max길이와 비교해서 최대길이 업데이트
7+
8+
nums의 길이 N
9+
TC : O(N^2)
10+
for문의 개수를 살펴보면 N-1 + N-2 + ... + 1 = (N-1)N/2 이므로 N^2/2 -> O(N^2)
11+
12+
SC : O(N)
13+
dp의 길이는 nums의 길이에 비례하므로
14+
"""
15+
16+
class Solution:
17+
def lengthOfLIS(self, nums: List[int]) -> int:
18+
dp = [1] * len(nums)
19+
max_LIS = 1
20+
for i in range(len(nums) - 1) :
21+
for j in range(i + 1, len(nums)) :
22+
if nums[i] < nums[j] and dp[i] + 1 > dp[j] :
23+
dp[j] = dp[i] + 1
24+
max_LIS = max(max_LIS, dp[j])
25+
return max_LIS

0 commit comments

Comments
 (0)