https://leetcode.com/problems/longest-continuous-increasing-subsequence/
https://leetcode.com/articles/longest-continuous-increasing-subsequence/
follow up1 要求subsequence中前后两个元素最⼤大间隔为1
https://github.com/mintycc/OnlineJudge-Solutions/blob/master/untag/LCIS%20Gap%20One/LCISGapOne.java
follow up2 要求subsequence中前后两个元素最⼤大间隔为k
https://github.com/mintycc/OnlineJudge-Solutions/blob/master/untag/LCIS%20Gap%20One/LCISGapK.java
follow up3 允许⼀一个break Example:
Input: [7, 3, 2, 3, 5, 6, 4, 2, 1]
Output: 5 - [2, 3, 5, 6, 4], [6, 4] is the break OR [3, 2, 3, 5, 6], [3,2] is the break
要有⼀一个变量量判断当前状态是否break过, 还有breaking index是另⼀一个 subarray的开始
Given an unsorted array of integers, find the length of longest
continuous
increasing subsequence (subarray).
Example 1:
Input: [1,3,5,4,7] 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.
Example 2:
Input: [2,2,2,2,2] Output: 1 Explanation: The longest continuous increasing subsequence is [2], its length is 1.
Note: Length of the array will not exceed 10,000
https://leetcode.com/articles/longest-continuous-increasing-subsequence/
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;
}
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/107352/Java-code-6-linerpublic int findLengthOfLCIS(int[] nums) {
if(nums.length==0) return 0;
int length=1,temp=1;
for(int i=0; i<nums.length-1;i++) {
if(nums[i]<nums[i+1]) {temp++; length=Math.max(length,temp);}
else temp=1;
}
return length;
}
follow up1 要求subsequence中前后两个元素最⼤大间隔为1
https://github.com/mintycc/OnlineJudge-Solutions/blob/master/untag/LCIS%20Gap%20One/LCISGapOne.java
public int findLengthOfLCIS(int[] nums) {
if (nums.length < 2) return nums.length;
int pp = 1;
int p = nums[0] < nums[1] ? 2 : 1;
int ans = p;
for (int i = 2; i < nums.length; i ++) {
int tmp = 1;
if (nums[i - 2] < nums[i] && pp + 1 > tmp) tmp = pp + 1;
if (nums[i - 1] < nums[i] && p + 1 > tmp) tmp = p + 1;
ans = Math.max(ans, tmp);
pp = p; p = tmp;
}
return ans;
}
https://github.com/mintycc/OnlineJudge-Solutions/blob/master/untag/LCIS%20Gap%20One/LCISGapK.java
public int findLengthOfLCIS(int[] nums, int k) {
if (nums.length == 0) return 0;
int[] f = new int[nums.length];
Arrays.fill(f, 1); // every number itself can be a shortest increasing subsequence
int ans = 1;
for (int i = 1; i < nums.length; i ++) {
for (int j = Math.max(0, i - k - 1); j < i; j ++) // the limit of gap k
if (nums[i] > nums[j]) // make sure subsequence is increasing
f[i] = Math.max(f[i], f[j] + 1);
ans = Math.max(ans, f[i]); // update result
}
return ans;
}
Input: [7, 3, 2, 3, 5, 6, 4, 2, 1]
Output: 5 - [2, 3, 5, 6, 4], [6, 4] is the break OR [3, 2, 3, 5, 6], [3,2] is the break
要有⼀一个变量量判断当前状态是否break过, 还有breaking index是另⼀一个 subarray的开始