Skip to content

Commit 308bde3

Browse files
Alagappan MaruthappanAlagappan Maruthappan
Alagappan Maruthappan
authored and
Alagappan Maruthappan
committed
longest increasing subsequence
1 parent 0c54e57 commit 308bde3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

longest_increaing_subsequence.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#https://leetcode.com/problems/longest-increasing-subsequence/description/
2+
3+
class Solution:
4+
def lengthOfLIS(self, nums):
5+
"""
6+
:type nums: List[int]
7+
:rtype: int
8+
"""
9+
N = len(nums)
10+
DP = [0] * N
11+
DP[0] = 1
12+
max_val = 1
13+
for i in range(N):
14+
this_max = 1
15+
for j in range(i + 1):
16+
if nums[j] < nums[i]:
17+
this_max = max(this_max, DP[j] + 1)
18+
DP[i] = this_max
19+
max_val = max(this_max, max_val)
20+
# print(DP)
21+
return max_val
22+
23+
24+
assert Solution().lengthOfLIS([10, 9, 2, 5, 3, 7, 101, 18]) == 4
25+
assert Solution().lengthOfLIS([1, 2]) == 2
26+
assert Solution().lengthOfLIS([-2, -1]) == 2

0 commit comments

Comments
 (0)