Skip to content

Commit f0489b1

Browse files
add tests for 334
1 parent 8384c12 commit f0489b1

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/main/java/com/fishercoder/solutions/_334.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@ public static class Solution2 {
3232
/**
3333
* Time: O(n)
3434
* Space: O(1)
35+
*
36+
* Idea: we can keep updating these two numbers:
37+
* firstSmallestNumber and secondSmallestNumber
38+
* until we found one that's bigger than both
3539
*/
3640
public boolean increasingTriplet(int[] nums) {
37-
int small = Integer.MAX_VALUE;
38-
int big = Integer.MAX_VALUE;
41+
int firstSmallestNumber = Integer.MAX_VALUE;
42+
int secondSmallestNumber = Integer.MAX_VALUE;
3943
for (int num : nums) {
40-
if (num <= small) {
41-
small = num;
42-
} else if (num <= big) {
43-
big = num;
44+
if (num <= firstSmallestNumber) {
45+
firstSmallestNumber = num;
46+
} else if (num <= secondSmallestNumber) {
47+
secondSmallestNumber = num;
4448
} else {
4549
return true;
4650
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._334;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _334Test {
10+
private static _334.Solution1 solution1;
11+
private static _334.Solution2 solution2;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _334.Solution1();
16+
solution2 = new _334.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(true, solution1.increasingTriplet(new int[]{2, 1, 5, 0, 4, 6}));
22+
assertEquals(true, solution2.increasingTriplet(new int[]{2, 1, 5, 0, 4, 6}));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(false, solution1.increasingTriplet(new int[]{5, 4, 3, 2, 1}));
28+
assertEquals(false, solution2.increasingTriplet(new int[]{5, 4, 3, 2, 1}));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals(false, solution1.increasingTriplet(new int[]{1, 1, -2, 6}));
34+
assertEquals(false, solution2.increasingTriplet(new int[]{1, 1, -2, 6}));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)