当前位置:网站首页>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
边栏推荐
- vant3+ts 封装uploader上传图片组件
- EASYCODE template
- Explanation of core interrupt of Godson processor
- 566. reshaping the matrix
- Advanced mountain -asp Net core router basic use demo 0.1
- A method of quickly reusing wechat login authorization in app
- Hangzhou AI developer meetup registration opens!
- USB转串口那些事儿—串口驱动类型
- C#的变量
- Goframe gredis configuration management | comparison of configuration files and configuration methods
猜你喜欢

NixOS 22.05安装过程记录

Arm64 stack backtracking

Use applet to quickly generate app in seven steps

General differences between SQL server versions released by Microsoft in different periods so far, for reference

Vant3 +ts packaged simple step advancer component

es7不使用父子和嵌套关系来实现一对多功能

JDBC quick start tutorial

Schéma de cristallisation différentielle active et différence entre LV - PECL, LVDS et hcsl
![Vulnhub[DC3]](/img/3a/1aa03e804d447d38e85807928fdb8f.png)
Vulnhub[DC3]

有源差分晶振原理圖以及LV-PECL、LVDS、HCSL區別
随机推荐
MySQL学习笔记
极限编程--根源分析实践
A method of quickly reusing wechat login authorization in app
二分查找的理解
Queue priority of message queue practice
73. 矩阵置零(标记法)
Cesium parabolic equation
grpc-swift入门
New media operation material website sharing enables you to create current affairs with half the effort
Global lock, table lock, row lock
Message queuing MySQL tables that store message data
First principles of enterprise architecture
es7不使用父子和嵌套关系来实现一对多功能
认识函数原创
idea 常用快捷键
Figma from getting started to giving up
Introduction to cookie usage
406. reconstruct the queue based on height
小程序+App,低成本获客及活跃的一种技术组合思路
Vant3+ts dropdownmenu drop-down menu, multi data can be scrolled