Skip to content

Commit b2912ea

Browse files
committed
add lengthOfLIS function to calculate the length of the longest increasing subsequence
1 parent ad4b1cb commit b2912ea

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
# Approach
3+
dp[n]은 nums[n] 위치까지 lis 길이입니다.
4+
5+
# Complexity
6+
nums의 길이를 n이라고 할 때,
7+
- Time complexity: 이중 반복문 O(N^2)
8+
- Space complexity: dp 배열 O(N)
9+
"""
10+
11+
12+
class Solution:
13+
def lengthOfLIS(self, nums: list[int]) -> int:
14+
n = len(nums)
15+
dp = [1] * n
16+
answer = 1
17+
for i in range(1, n):
18+
for j in range(i):
19+
if nums[j] < nums[i]:
20+
dp[i] = max(dp[j] + 1, dp[i])
21+
answer = max(answer, dp[i])
22+
return answer

0 commit comments

Comments
 (0)