Longest Continuous Increasing Subsequence(最长递增连续子序列)
题目:
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
给定一个无序的整型数组,找到最长的连续递增子序列。
例1:
输入:[1,3,5,4,7]
输出:3
例2:
输入:[2,2,2,2,2]
输出:1
解法:时间复杂度O(N)
Every (continuous) increasing subsequence is disjoint, and the boundary of each such subsequence occurs whenever nums[i-1] >= nums[i]. When it does, it marks the start of a new increasing subsequence at nums[i], and we store such i in the variable anchor.
For example, if nums = [7, 8, 9, 1, 2, 3], then anchor starts at 0 (nums[anchor] = 7) and gets set again to anchor = 3 (nums[anchor] = 1). Regardless of the value of anchor, we record a candidate answer of i - anchor + 1, the length of the subarray nums[anchor], nums[anchor+1], ..., nums[i]; and our answer gets updated appropriately.
每一个连续递增子序列都是不相交的,并且当nums[i-1]>=nums[i]时,每个子序列的边界产生。我们可以给一个新产生的递增序列的起始点记为nums[i],然后将此时的i存为一个标记变量a。
比如,数组为[7,8,9,1,2,3],那么标记变量a为0(nums[a]=7),然后下一个标记变量a为3(nums[a]=1)。不管标记变量a的值为多少,我们可以将“i-a+1”的值记为子序列nums[a],nums[a+1],...,nums[i]的长度且结果会自适应的更新。
官方代码(Java):
class Solution {public int findLengthOfLCIS(int[] nums) {int ans = 0, anchor = 0;for (int i = 0; i < nums.length; ++i) {if (i > 0 && nums[i-1] >= nums[i]) anchor = i;ans = Math.max(ans, i - anchor + 1);}return ans;}
}
复杂度分析:
Time Complexity: O(N),where N is the length of nums.
Space Complexity: O(1), the space used by anchor and ans.
时间复杂度:O(N),N是数组的长度。
空间复杂度:O(1),空间使用anchor和ans两个变量。
个人代码(C++):
class Solution {
public:int findLengthOfLCIS(vector<int>& nums) {int l = 1;int ans = l;int len = nums.size();int i = 0;if (len == 0)return 0;for(i=1;i<len;i++){if(nums[i-1]<nums[i]){l++;ans=max(ans,l);}else l = 1; }return ans;}
};
Longest Continuous Increasing Subsequence(最长递增连续子序列)相关推荐
- 674. Longest Continuous Increasing Subsequence最长连续递增子数组
[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...
- C#LeetCode刷题之#674-最长连续递增序列( Longest Continuous Increasing Subsequence)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3734 访问. 给定一个未经排序的整数数组,找到最长且连续的的递增 ...
- C++longest increasing subsequence 最长递增子序列的实现之二(附完整源码)
C++longest increasing subsequence 最长递增子序列的实现 C++longest increasing subsequence 最长递增子序列的的实现完整源码(定义,实现 ...
- C++longest increasing subsequence 最长递增子序列的实现之一(附完整源码)
C++longest increasing subsequence 最长递增子序列的实现 C++longest increasing subsequence 最长递增子序列的的实现完整源码(定义,实现 ...
- LeetCode 674. Longest Continuous Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...
- 674 Longest Continuous Increasing Subsequence(每日一题)
674:Longest Continuous Increasing Subsequence 每日一题的第一天 解题思路 双重循环 class Solution {public int findLeng ...
- LeetCode 674. Longest Continuous Increasing Subsequence--python,Java,C++解法
此题链接:Longest Continuous Increasing Subsequence - LeetCode Given an unsorted array of integers, find ...
- C++longest palindromic subsequence最长回文子序列算法实现(附完整源码)
C++longest palindromic subsequence最长回文子序列算法 C++longest palindromic subsequence最长回文子序列算法实现完整源码(定义,实现, ...
- BNUOJ 4215 最长公共连续子序列
最长公共连续子序列 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class na ...
- lintcode 最长上升连续子序列 II(二维最长上升连续序列)
题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...
最新文章
- 小程序真机调试访问不了接口_小程序入门
- jdk8新特性_JDK8与JDK9新特性学习
- html阅读开放试用阶段,泰克为不同行业提供100种应用功能免费试用
- vivo计算机的隐藏功能介绍,vivo手机有哪些隐藏功能?这6个功能实在太好用了,要悄悄用起来...
- dubbo的监控中心
- mac android sdk manager速度慢,android - SDK Manager无法在Mac上打开 - 堆栈内存溢出
- 最直白的跨域访问原理
- 走进我的交易室01_引子
- 移除collection中元素的注意事项(应用collection.remove移除元素造成的错误)
- python bottle 终止返回_关于python的bottle框架跨域请求报错问题的处理
- kafka应用场景_从未如此简单:10分钟带你逆袭Kafka!
- 麦克马斯特大学计算机的强项,阿尔伯塔大学和麦克马斯特大学哪所学校好?
- iphone 价格的秘密
- 一 马尔可夫决策问题
- SPN实现——限时1000ms的代换-置换网络加解密的时间优化思路
- postgresql安装教程(Windows)
- 得到QB的20种方法
- Bokeh可视化图表使用教程
- linux系统移植与开发
- 一文读懂“+区块链”与“区块链+”的区别