Skip to content

Commit 434118e

Browse files
update 904
1 parent a763c36 commit 434118e

File tree

2 files changed

+38
-26
lines changed
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

2 files changed

+38
-26
lines changed

src/main/java/com/fishercoder/solutions/firstthousand/_904.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,32 @@
55

66
public class _904 {
77
public static class Solution1 {
8-
public int totalFruit(int[] tree) {
8+
/**
9+
* This is a two-pointer solution, a.k.a sliding window
10+
*/
11+
public int totalFruit(int[] fruits) {
912
int maxFruits = 0;
1013
Set<Integer> set = new HashSet<>();
1114
int startIndex = 0;
12-
for (int i = 0; i < tree.length; i++) {
13-
if (set.size() < 2 || set.contains(tree[i])) {
14-
set.add(tree[i]);
15-
maxFruits = Math.max(maxFruits, i - startIndex + 1);
15+
for (int i = 0; i < fruits.length; i++) {
16+
if (set.size() < 2 || set.contains(fruits[i])) {
17+
//either one of the two cases, we keep adding fruits[i] into the set and expand i to the right
1618
} else {
17-
int lastOne = tree[i - 1];
19+
//in other cases, we know there's a 3rd type of fruit we just encountered,
20+
// so keep the 2nd type of fruit as lastOne, go backwards,
21+
// find the first 1st type of fruit as j, set startIndex = j + 1,
22+
// remove the 1st type of fruit from the set and break
23+
int lastOne = fruits[i - 1];
1824
for (int j = i - 2; j >= 0; j--) {
19-
if (tree[j] != lastOne) {
25+
if (fruits[j] != lastOne) {
2026
startIndex = j + 1;
21-
set.remove(tree[j]);
27+
set.remove(fruits[j]);
2228
break;
2329
}
2430
}
25-
set.add(tree[i]);
26-
maxFruits = Math.max(maxFruits, i - startIndex + 1);
2731
}
32+
set.add(fruits[i]);
33+
maxFruits = Math.max(maxFruits, i - startIndex + 1);
2834
}
2935
return maxFruits;
3036
}
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,53 @@
11
package com.fishercoder.firstthousand;
22

33
import com.fishercoder.solutions.firstthousand._904;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _904Test {
1010
private static _904.Solution1 solution1;
11-
private static int[] tree;
11+
private static int[] fruits;
1212

13-
@BeforeClass
14-
public static void setup() {
13+
@BeforeEach
14+
public void setup() {
1515
solution1 = new _904.Solution1();
1616
}
1717

1818
@Test
1919
public void test1() {
20-
tree = new int[]{1, 2, 1};
21-
assertEquals(3, solution1.totalFruit(tree));
20+
fruits = new int[]{1, 2, 1};
21+
assertEquals(3, solution1.totalFruit(fruits));
2222
}
2323

2424
@Test
2525
public void test2() {
26-
tree = new int[]{0, 1, 2, 2};
27-
assertEquals(3, solution1.totalFruit(tree));
26+
fruits = new int[]{0, 1, 2, 2};
27+
assertEquals(3, solution1.totalFruit(fruits));
2828
}
2929

3030
@Test
3131
public void test3() {
32-
tree = new int[]{1, 2, 3, 2, 2};
33-
assertEquals(4, solution1.totalFruit(tree));
32+
fruits = new int[]{1, 2, 3, 2, 2};
33+
assertEquals(4, solution1.totalFruit(fruits));
3434
}
3535

3636
@Test
3737
public void test4() {
38-
tree = new int[]{3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4};
39-
assertEquals(5, solution1.totalFruit(tree));
38+
fruits = new int[]{3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4};
39+
assertEquals(5, solution1.totalFruit(fruits));
4040
}
4141

4242
@Test
4343
public void test5() {
44-
tree = new int[]{0, 1, 6, 6, 4, 4, 6};
45-
assertEquals(5, solution1.totalFruit(tree));
44+
fruits = new int[]{0, 1, 6, 6, 4, 4, 6};
45+
assertEquals(5, solution1.totalFruit(fruits));
46+
}
47+
48+
@Test
49+
public void test6() {
50+
fruits = new int[]{0, 1, 1, 4, 3};
51+
assertEquals(3, solution1.totalFruit(fruits));
4652
}
4753
}

0 commit comments

Comments
 (0)