当前位置:网站首页>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
边栏推荐
- C#的变量
- 118. 杨辉三角(动态规划)
- Are Huishang futures accounts reliable? Is the fund safe?
- Codeforces Round #398 (Div. 2) D. Cartons of milk
- Arm64 stack backtracking
- A method of quickly reusing wechat login authorization in app
- 73. matrix zeroing (marking method)
- Continued 2 asp Net core router basic use demonstration 0.2 acquisition of default controller data
- Original error interface
- SSM集成FreeMarker以及常用语法
猜你喜欢

Queue priority of message queue practice

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

Notes on user experience elements: user centered product design

Vant3+ts encapsulates uploader upload image component

ESP32-C3 ESP-IDF 配置smartconfig 和 sntp 获取网络时间

Arm64 Stack backtrack

JDBC quick start tutorial

小程序+App,低成本获客及活跃的一种技术组合思路

极限编程--根源分析实践

Continued 2 asp Net core router basic use demonstration 0.2 acquisition of default controller data
随机推荐
leetcode 674 最长递增子串
有源差分晶振原理圖以及LV-PECL、LVDS、HCSL區別
118. Yanghui triangle (dynamic planning)
SQL游标(cursor)详细说明及内部循环使用示例
Codeforces Round #398 (Div. 2) D. Cartons of milk
Window版本pytorch入门深度学习环境安装与配置
进阶之大山-asp.net core 路由程序基础使用演示0.1
Stream flow precautions
leetcode491 递增子序列
EasyCode模板
用好IDE,研发效能提速100%
Kali2022安装armitage
[csp]202012-2 optimal threshold for period end forecast
Vulnhub[DC3]
在同花顺开户证券安全吗
TensorFlow求梯度时提示TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘
关于数据集
山东大学软件学院项目实训-创新实训-山大软院网络攻防靶场实验平台(二十五)-项目个人总结
C#操作数据库增查业务数据值内容案例学校表
使用MongoDB官方go库操作MongoDB原创