【Leetcode】300—Longest|【Leetcode】300—Longest Increasing Subsequence
一、题目描述
给定一个无序的整数数组,找到其中最长上升子序列的长度。
示例:
输入: [10,9,2,5,3,7,101,18]
输出: 4
解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。
二、代码实现 方法:动态规划法 LISList[i]表示到第i位时最长上升序列的长度。显然LISList[0] = 1。对于任意的i不为零的情况,应该在i的左侧找一个下标 j ,其满足两个条件:
- nums[ j ]比 nums[ i ] 小
- 它是所有满足条件1里 dp [j] 最大的那个
LISList[i] = max(LISList[j]) + 1
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
LISList = [1] * len(nums)
for i in range(1, len(nums)):
LIS = 0
for j in range(i):
if nums[j] >= nums[i]: continue
if LISList[j] > LIS:
LIS = LISList[j]
if LIS:
LISList[i] = LIS + 1
return max(LISList)
推荐阅读
- 宽容谁
- 我要做大厨
- 增长黑客的海盗法则
- 画画吗()
- 2019-02-13——今天谈梦想()
- 远去的风筝
- 三十年后的广场舞大爷
- 叙述作文
- 20190302|20190302 复盘翻盘
- 学无止境,人生还很长