Skip to content

Commit d1f30f8

Browse files
add 674
1 parent 8e84e6c commit d1f30f8

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 674. Longest Continuous Increasing Subsequence
5+
* Given an unsorted array of integers, find the length of longest continuous increasing subsequence.
6+
7+
Example 1:
8+
Input: [1,3,5,4,7]
9+
Output: 3
10+
Explanation: The longest continuous increasing subsequence is [1,3,5],
11+
its length is 3. Even though [1,3,5,7] is also an increasing subsequence,
12+
it's not a continuous one where 5 and 7 are separated by 4.
13+
14+
Example 2:
15+
Input: [2,2,2,2,2]
16+
Output: 1
17+
Explanation: The longest continuous increasing subsequence is [2], its length is 1.
18+
19+
Note: Length of the array will not exceed 10,000.
20+
*/
21+
public class _674 {
22+
public static class Solution1 {
23+
public int findLengthOfLCIS(int[] nums) {
24+
int longest = 0;
25+
for (int i = 0; i < nums.length; i++) {
26+
int len = 1;
27+
for (int j = i + 1; j < nums.length; j++) {
28+
if (nums[j - 1] < nums[j]) {
29+
len++;
30+
continue;
31+
} else {
32+
break;
33+
}
34+
}
35+
longest = Math.max(longest, len);
36+
}
37+
return longest;
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._674;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _674Test {
10+
private static _674.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _674.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1, 3, 5, 4, 7};
21+
assertEquals(3, solution1.findLengthOfLCIS(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{2, 2, 2, 2, 2};
27+
assertEquals(1, solution1.findLengthOfLCIS(nums));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)