LeetCode - Algorithms - 674. Longest Continuous Increasing Subsequence

Problem

674. Longest Continuous Increasing Subsequence

Java

Sliding Window

Sliding Window problems are a subset of dynamic programming problems, though the approach to solving them is quite different from the one used in solving tabulation or memoization problems.

© Approach 1: Sliding Window

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public int findLengthOfLCIS(int[] nums) {
int maxLen = 0;
int anchor = 0;
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] <= nums[i - 1]) anchor = i;
maxLen = Math.max(maxLen, i - anchor + 1);
}
return maxLen;
}
}

Submission Detail

  • 36 / 36 test cases passed.
  • Runtime: 1 ms, faster than 98.29% of Java online submissions for Longest Continuous Increasing Subsequence.
  • Memory Usage: 39.7 MB, less than 70.15% of Java online submissions for Longest Continuous Increasing Subsequence.

my solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int findLengthOfLCIS(int[] nums) {
final int N = nums.length;
if (N < 2)
return N;
int len = 1, tempLen = 1;
for (int i = 1; i < N; i++) {
if (nums[i] > nums[i - 1]) {
tempLen++;
} else {
if (tempLen > len)
len = tempLen;
tempLen = 1;
}
}
if (tempLen > len)
len = tempLen;
return len;
}
}

Submission Detail

  • 36 / 36 test cases passed.
  • Runtime: 1 ms, faster than 98.29% of Java online submissions for Longest Continuous Increasing Subsequence.
  • Memory Usage: 39.9 MB, less than 41.25% of Java online submissions for Longest Continuous Increasing Subsequence.