-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlis.py
More file actions
31 lines (24 loc) · 685 Bytes
/
lis.py
File metadata and controls
31 lines (24 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def lis(nums):
dp = [ 1 for _ in range(len(nums)) ]
for i in range(len(nums) - 1, -1, -1):
for j in range(i + 1, len(nums)):
if nums[j] > nums[i]:
dp[i] = max(1, dp[i], 1 + dp[j])
return max(dp)
dp = { len(nums) - 1: 1 }
def dfs(i):
if i in dp:
return dp[i]
if i == len(nums):
return 0
res = 1
for j in range(i, len(nums)):
if nums[j] > nums[i]:
res = max(res, 1 + dfs(j))
dp[i] = res
return res
res = 1
for i in range(len(nums)):
res = max(res, dfs(i))
return res
print(lis([10,9,2,5,3,7,101,18]))