0
点赞
收藏
分享

微信扫一扫

【LeetCode】300.最长上升子序列

1.​​题目​​

2.思想

dp题

  • 状态:设​​dp[i]​​​ 表示以​​nums[i]​​ 为 结尾得到的最大的上升子序列长度
  • 策略:如何得到状态转移公式?
    ​​​dp[i]​​​ 会 依赖i前的所有数j,其中【LeetCode】300.最长上升子序列_leetcode,递推式便是
    【LeetCode】300.最长上升子序列_子序列_02
    不断更新取最大就行了

3.代码

class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [1] * len(nums) # 自身都是1
# s 表示的是区间长度
for i in range(0,len(nums)):
for j in range(0,i):
if nums[i] > nums[j]:
dp[i] = max(dp[j]+1,dp[i])
res = dp[0]
for i in range(len(nums)):
res = max(res,dp[i])
return res


举报

相关推荐

0 条评论