Skip to content

Commit 5a0d6b9

Browse files
authored
Update Longest Continuous Increasing Subsequence.java
1 parent 6468380 commit 5a0d6b9

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Java/Longest Continuous Increasing Subsequence.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ Given an unsorted array of integers, find the length of longest continuous incre
3333
Note: Length of the array will not exceed 10,000.
3434
*/
3535

36+
// simply dp[i] = 1; dp[i] += dp[i - 1] if nums[i] > nums[i-1]
37+
class Solution {
38+
public int findLengthOfLCIS(int[] nums) {
39+
if (nums == null || nums.length == 0) {
40+
return 0;
41+
}
42+
int n = nums.length;
43+
int[] dp = new int[2];
44+
dp[0] = 1;
45+
int max = 1;
46+
for (int i = 1; i < n; i++) {
47+
dp[i % 2] = 1;
48+
if (nums[i] > nums[i - 1]) {
49+
dp[i % 2] += dp[(i - 1) % 2];
50+
}
51+
max = Math.max(max, dp[i % 2]);
52+
}
53+
return max;
54+
}
55+
}
3656

3757
/*
3858
Thoughts:
@@ -110,4 +130,4 @@ public int findLengthOfLCIS(int[] nums) {
110130
}
111131
}
112132

113-
```
133+
```

0 commit comments

Comments
 (0)