Skip to content

Commit f79d6e4

Browse files
add 904
1 parent f92337a commit f79d6e4

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ _If you like this project, please leave me a star._ ★
317317
|912|[Sort an Array](https://leetcode.com/problems/sort-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_912.java) | |Easy|
318318
|908|[Smallest Range I](https://leetcode.com/problems/smallest-range-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_908.java) | |Easy|
319319
|901|[Online Stock Span](https://leetcode.com/problems/online-stock-span/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_901.java) | |Medium| Stack
320+
|904|[Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_904.java) | |Medium| Two Pointers
320321
|900|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | |Medium|
321322
|897|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_897.java) | |Easy| DFS, recursion
322323
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | |Easy|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class _904 {
7+
public static class Solution1 {
8+
public int totalFruit(int[] tree) {
9+
int maxFruits = 0;
10+
Set<Integer> set = new HashSet<>();
11+
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);
16+
} else {
17+
int lastOne = tree[i - 1];
18+
for (int j = i - 2; j >= 0; j--) {
19+
if (tree[j] != lastOne) {
20+
startIndex = j + 1;
21+
set.remove(tree[j]);
22+
break;
23+
}
24+
}
25+
set.add(tree[i]);
26+
maxFruits = Math.max(maxFruits, i - startIndex + 1);
27+
}
28+
}
29+
return maxFruits;
30+
}
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._904;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _904Test {
10+
private static _904.Solution1 solution1;
11+
private static int[] tree;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _904.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
tree = new int[]{1, 2, 1};
21+
assertEquals(3, solution1.totalFruit(tree));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
tree = new int[]{0, 1, 2, 2};
27+
assertEquals(3, solution1.totalFruit(tree));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
tree = new int[]{1, 2, 3, 2, 2};
33+
assertEquals(4, solution1.totalFruit(tree));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
tree = new int[]{3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4};
39+
assertEquals(5, solution1.totalFruit(tree));
40+
}
41+
42+
@Test
43+
public void test5() {
44+
tree = new int[]{0, 1, 6, 6, 4, 4, 6};
45+
assertEquals(5, solution1.totalFruit(tree));
46+
}
47+
}

0 commit comments

Comments
 (0)