当前位置:网站首页>leetcode 674 最长递增子串
leetcode 674 最长递增子串
2022-06-12 17:51:00 【zhuxiaohai68】
给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
连续递增的子序列 可以由两个下标 l 和 r(l < r)确定,如果对于每个 l <= i < r,都有 nums[i] < nums[i + 1] ,那么子序列 [nums[l], nums[l + 1], …, nums[r - 1], nums[r]] 就是连续递增子序列。
示例 1:
输入:nums = [1,3,5,4,7]
输出:3
解释:最长连续递增序列是 [1,3,5], 长度为3。
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为 5 和 7 在原数组里被 4 隔开。
示例 2:
输入:nums = [2,2,2,2,2]
输出:1
解释:最长连续递增序列是 [2], 长度为1。
可以用动态规划的思想来理解。就是设dp[i]代表以nums[i]结尾的最长递增后缀。
- 如果nums[i] > nums[i-1]则dp[i] = [i-1] + 1
- 否则,nums[i-1]和nums[i]构不成递增关系,以nums[i]结尾的最长递增后缀为1,也就是nums[i]本身
边界条件dp[i]=1,也就是以任何一个字符为结尾的最长递增后缀最小也可以取1
class Solution(object):
def findLengthOfLCIS(self, nums):
""" :type nums: List[int] :rtype: int """
n = len(nums)
dp = [1]*n
for i in range(n):
if i > 0 and nums[i] > nums[i-1]:
dp[i] = dp[i-1] + 1
return max(dp)
class Solution(object):
def findLengthOfLCIS(self, nums):
""" :type nums: List[int] :rtype: int """
n = len(nums)
start = 0
tot = 1
for i in range(n):
if i > 0 and nums[i] > nums[i-1]:
pass
else:
start = i
tot = max(tot, i-start+1)
return tot
边栏推荐
- 406. reconstruct the queue based on height
- AlibabaProtect. How to delete and uninstall exe
- 有关cookie的用法介绍
- 赛程更新| 2022微软与英特尔黑客松大赛火热报名中
- I heard that distributed IDS cannot be incremented globally?
- SqlServer常用语句及函数
- Byte flybook Human Resources Kit three sides
- Authorization in Golang ProjectUseing Casbin
- Continued 2 asp Net core router basic use demonstration 0.2 acquisition of default controller data
- Error record: illegalstateexception: optional int parameter 'XXXX' is
猜你喜欢

php 实现无限极分类树(递归及其优化)

用好IDE,研发效能提速100%

小程序和App同时拥有?两者兼得的一种技术方案

Arm64栈回溯

C operation database added business data value content case school table

SSM集成FreeMarker以及常用语法

淘宝Native研发模式的演进与思考 | DX研发模式

Vant3+ts encapsulates uploader upload image component

赛程更新| 2022微软与英特尔黑客松大赛火热报名中

Vant3+ts dropdownmenu drop-down menu, multi data can be scrolled
随机推荐
Detailed description of SQL cursor and example of internal recycling
Lambda - 1
Applet and app are owned at the same time? A technical scheme with both
Array sorts in the specified order
Authorization in Golang ProjectUseing Casbin
Unprecedented analysis of Milvus source code architecture
Graphical data model for system design
vant3+ts DropdownMenu 下拉菜单,数据多能滚动加载
一物一码追踪溯源系统介绍
山东大学软件学院项目实训-创新实训-山大软院网络攻防靶场实验平台(二十五)-项目个人总结
Cesium抛物线方程
vant3 +ts 封装简易step进步器组件
TensorFlow从网络读取数据
用好IDE,研发效能提速100%
566. reshaping the matrix
续2 asp.net core 路由程序基础使用演示0.2 默认控制器数据的获取到
EASYCODE template
idea 常用快捷键
Hangzhou AI developer meetup registration opens!
406. 根据身高重建队列