Skip to content

Commit 92cabef

Browse files
committed
feat: add solution for longest increasing subsequence in python
1 parent 7c254f5 commit 92cabef

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List
2+
3+
def lengthOfLIS( nums: List[int]) -> int:
4+
if not nums:
5+
return 0
6+
dp = [1] * len(nums)
7+
# Dynamic programming - Bottom up approach - O(n^2)
8+
for i in range(len(nums)):
9+
for j in range(i):
10+
if nums[i] > nums[j]:
11+
dp[i] = max( dp[i], dp[j] + 1)
12+
return max(dp)
13+
14+
assert lengthOfLIS([10,9,2,5,3,7,101,18]) == 4
15+
assert lengthOfLIS([0,1,0,3,2,3]) == 4

0 commit comments

Comments
 (0)