Poison

674. Longest Continuous Increasing Subsequence

DP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int findLengthOfLCIS(int[] nums) {
// 定义 dp[i] 为以 nums[i] 结尾的最长连续递增子序列的长度
int[] dp = new int[nums.length];
Arrays.fill(dp, 1);

int maxLength = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1]) {
dp[i] = dp[i - 1] + 1;
}
maxLength = Math.max(maxLength, dp[i]);
}

return maxLength;
}
}
Greedy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int findLengthOfLCIS(int[] nums) {
int maxLength = 1;

int i = 0;
for (int j = 1; j < nums.length; j++) {
// LCIS: [i, j]
if (nums[j] > nums[j - 1]) {
maxLength = Math.max(maxLength, j - i + 1);
} else {
i = j;
}
}

return maxLength;
}
}
Reference

674. Longest Continuous Increasing Subsequence