当前位置:网站首页>Leetcode 674 longest incrementing substring
Leetcode 674 longest incrementing substring
2022-06-12 17:59:00 【zhuxiaohai68】
Given an unordered array of integers , Find the longest and Successive increasing subsequences , And return the length of the sequence .
Successive increasing subsequences It can be made up of two subscripts l and r(l < r) determine , If for each l <= i < r, There are nums[i] < nums[i + 1] , So the subsequence [nums[l], nums[l + 1], …, nums[r - 1], nums[r]] It's a continuous increasing subsequence .
Example 1:
Input :nums = [1,3,5,4,7]
Output :3
explain : The longest continuous increasing sequence is [1,3,5], The length is 3.
Even though [1,3,5,7] It's also a subsequence of ascending order , But it's not continuous , because 5 and 7 In the original array is 4 separate .
Example 2:
Input :nums = [2,2,2,2,2]
Output :1
explain : The longest continuous increasing sequence is [2], The length is 1.
It can be understood with the idea of dynamic programming . That's to say dp[i] Representative to nums[i] The longest incrementing suffix at the end .
- If nums[i] > nums[i-1] be dp[i] = [i-1] + 1
- otherwise ,nums[i-1] and nums[i] Cannot form an increasing relationship , With nums[i] The longest incrementing suffix at the end is 1, That is to say nums[i] In itself
The boundary conditions dp[i]=1, That is, the longest incrementing suffix ending in any character can be the smallest 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
边栏推荐
- High-Speed Layout Guidelines 未完...
- 字节飞书人力资源套件三面
- vant3+ts+pinia tab选项卡列表页面点击进详情,详情页返回tab高亮在原位置,刷新高亮默认在第一项
- An easy-to-use IDE for small programs
- 73. 矩阵置零(标记法)
- Introduction to cookie usage
- [csp]202012-2 optimal threshold for period end forecast
- Byte flybook Human Resources Kit three sides
- 极限编程--根源分析实践
- 一种好用、易上手的小程序IDE
猜你喜欢
vant3+ts 封装uploader上传图片组件
Arm64 Stack backtrack
Nixos 22.05 installation process record
Risc-v ide mounriver studio v1.60 update point introduction
C#简单介绍
es7不使用父子和嵌套关系来实现一对多功能
Make good use of IDE, speed up R & D efficiency by 100%
Cesium parabolic equation
Queue priority of message queue practice
WinForm, crystal report making
随机推荐
Byte flybook Human Resources Kit three sides
TensorFlow2训练数据集的两种方式
Lightweight and convenient small program to app technology solution to realize interconnection with wechat / traffic app
Are Huishang futures accounts reliable? Is the fund safe?
Vant3+ts encapsulates uploader upload image component
极限编程--根源分析实践
Deep interest evolution network for click through rate prediction
Cesium parabolic equation
An easy-to-use IDE for small programs
消息队列实战之队列优先级
JDBC快速入门教程
使用MongoDB官方go库操作MongoDB原创
Arm64 Stack backtrack
[CSP]202012-2期末预测之最佳阈值
Tensorflow reads data from the network
Research results of low code platform
A method of quickly reusing wechat login authorization in app
PHP implementation of infinite classification tree (recursion and Optimization)
Advanced mountain -asp Net core router basic use demo 0.1
Article name