674.|674. Longest Continuous Increasing Subsequence 最长连续递增序列
题目链接
tag:
-Easy;
- Sliding Window;
??Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
Example 1:
Input: [1,3,5,4,7]Example 2:
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.
Input: [2,2,2,2,2]Note: Length of the array will not exceed 10,000.
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1.
【674.|674. Longest Continuous Increasing Subsequence 最长连续递增序列】思路:
??比较简单,滑动窗口,只要一个辅助变量当作窗口左边界,如果当前一个元素 num[i] 比 num[i-1] 大说明窗口增加,否者重新开始窗口。代码如下:
class Solution {
public:
int findLengthOfLCIS(vector& nums) {
// 滑动窗口
if (nums.empty())
return 0;
if (nums.size() == 1)
return 1;
int res = 1, left = -1;
for (int i=1;
i nums[i-1])
res = max(res, i-left);
else
left = i - 1;
}
return res;
}
};
推荐阅读
- LeetCode(03)Longest|LeetCode(03)Longest Substring Without Repeating Characters
- python|leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法
- For|For many, life's longest mile is the stretch from dependence to independence
- 动态规划|Longest Common Subsequence(入门dp题)
- Leetcode-549. Binary Tree Longest Consecutive Sequence II
- 32.|32. Longest Valid Parentheses
- [Lintcode] Longest Increasing Subsequence 最长上升序列
- 强化学习之MountainCarContinuous(注册自己的gym环境)
- Michael|12.3 ANCOVA with two factors and one continuous covariate
- Success|Success is a continuous journey