当前位置:网站首页>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
边栏推荐
- vant3+ts 封装uploader上传图片组件
- 迄今微软不同时期发布的SQL Server各版本之间的大致区别,供参考查阅
- vant3+ts+pinia tab选项卡列表页面点击进详情,详情页返回tab高亮在原位置,刷新高亮默认在第一项
- Learn the mitmproxy packet capturing tool from scratch
- Tidb Hackathon 2021 - pcloud: conduct icloud pcloud team interview on the database
- Common dependencies of SSM
- Implementation of asynchronous query of Flink dimension table and troubleshooting
- 406. 根据身高重建队列
- An easy-to-use IDE for small programs
- Bug record: data truncation: incorrect datetime value:
猜你喜欢

Make good use of IDE, speed up R & D efficiency by 100%

Deep interest evolution network for click through rate prediction

Unprecedented analysis of Milvus source code architecture

The server time zone value ‘� й ��� ʱ ��‘ is unrecognized or represents more than one time zone. ......

An easy-to-use IDE for small programs

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

The server time zone value ‘�й���ʱ��‘ is unrecognized or represents more than one time zone. ......

Yyds dry goods inventory leetcode question set 911 - 920

Cesium parabolic equation

龙芯处理器内核中断讲解
随机推荐
String s = null ; String s = new String(); String s = "; what is the difference between string s?
Detailed description of SQL cursor and example of internal recycling
Codeforces Round #398 (Div. 2) D. Cartons of milk
Yyds dry goods inventory leetcode question set 911 - 920
进阶之大山-asp.net core 路由程序基础使用演示0.1
小程序+App,低成本获客及活跃的一种技术组合思路
vant3+ts+pinia tab选项卡列表页面点击进详情,详情页返回tab高亮在原位置,刷新高亮默认在第一项
73. 矩阵置零(标记法)
Graphical data model for system design
qemu+gdb小节
Lambda - 1
轻量、便捷的小程序转App技术方案,实现与微信/流量App互联互通
Introduction to cookie usage
String的split方法的使用
An easy-to-use IDE for small programs
Extreme Programming -- Practice of root cause analysis
Cesium抛物线方程
小程序和App同时拥有?两者兼得的一种技术方案
赛程更新| 2022微软与英特尔黑客松大赛火热报名中
Vulnhub[DC3]