Given an unsorted array of integers, find the longest continuously increasing subsequence and return the length of that sequence.
Continuously increasing subsequences can be determined by two subscripts l and r (l < r), if for each l <= i < r, there are nums[i] < nums[i + 1], then the subsequence [ nums[l], nums[l + 1], …, nums[r - 1], nums[r]] are continuous increasing subsequences.
Example 1:
Input: nums = [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing sequence is [1,3,5] with a length of 3.
Although [1,3,5,7] is also an ascending subsequence, it is not continuous because 5 and 7 are separated by 4 in the original array.
Example 2:
Input: nums = [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing sequence is [2] with length 1.
Hints:
0 <= nums.length <= 104
-109 <= nums[i] <= 109
Passes 59,879
Commits 127,426
Source: LeetCode
Link: https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence
The copyright belongs to Leetcode Network. For commercial reprints, please contact the official authorization, for non-commercial reprints, please indicate the source.
Problem-solving idea: prepare a pointer p1, and prepare two integers a, b, a is used to record the length of the currently known longest incremental subsequence, b is used to record the length of the incremental subsequence currently being traversed; finally a = max(a,b), just output a; code implementation:
public class January24 {
public int findLengthOfLCIS(int[] nums) {
if (nums.length == 0) return 0;
int k = 1;
int p = 1;
for (int i = 1; i < nums.length; ++i) {
if (nums[i] > nums[i - 1]) {
++p;
k = Math.max(k, p);
continue;
}
p = 1;
}
return k;
}
}
I just want to know what is the code of the 78% who beat me
Official solution code:
class Solution {
public int findLengthOfLCIS(int[] nums) {
int ans = 0;
int n = nums.length;
int start = 0;
for (int i = 0; i < n; i++) {
if (i > 0 && nums[i] <= nums[i - 1]) {
start = i;
}
ans = Math.max(ans, i - start + 1);
}
return ans;
}
}
// 作者:LeetCode-Solution
// 链接:https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/solution// // /zui-chang-lian-xu-di-zeng-xu-lie-by-leet-dmb8/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。