/**
* nums [1, 3, 5, 4, 7]
* dp
* [1] -> 1
* [1,3] -> 2
* [1,3,5] -> 3
* [1,3,5,4] -> 1
* [1,3,5,4,7] -> 2
*
* maxLen -> 3
*/
public int findLengthOfLCIS(int[] nums) {
int[] length= new int[nums.length];
Arrays.fill(length, 1);
for (int i = 1; i < nums.length; i++) {
if(nums[i] > nums[i-1]) {
length[i] = length[i-1] + 1;
}
}
int maxLen = 0;
for (int len : length) {
maxLen = Math.max(maxLen, len);
}
return maxLen;
}
- dp 优化 时间复杂度O(N), 空间复杂度O(1)
public int findLengthOfLCIS(int[] nums) { //类似滑动窗口
if (nums.length<=0) return 0;
int before = 1;
int maxLen = 1;
for (int i = 1; i < nums.length; i++) {
if(nums[i] > nums[i-1]) {
before = before + 1;
maxLen = Math.max(maxLen, before);
} else {
before = 1;
}
}
return maxLen;
}