Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf79d6e4

Browse files
add 904
1 parentf92337a commitf79d6e4

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashSet;
4+
importjava.util.Set;
5+
6+
publicclass_904 {
7+
publicstaticclassSolution1 {
8+
publicinttotalFruit(int[]tree) {
9+
intmaxFruits =0;
10+
Set<Integer>set =newHashSet<>();
11+
intstartIndex =0;
12+
for (inti =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+
intlastOne =tree[i -1];
18+
for (intj =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+
returnmaxFruits;
30+
}
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._904;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_904Test {
10+
privatestatic_904.Solution1solution1;
11+
privatestaticint[]tree;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_904.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
tree =newint[]{1,2,1};
21+
assertEquals(3,solution1.totalFruit(tree));
22+
}
23+
24+
@Test
25+
publicvoidtest2() {
26+
tree =newint[]{0,1,2,2};
27+
assertEquals(3,solution1.totalFruit(tree));
28+
}
29+
30+
@Test
31+
publicvoidtest3() {
32+
tree =newint[]{1,2,3,2,2};
33+
assertEquals(4,solution1.totalFruit(tree));
34+
}
35+
36+
@Test
37+
publicvoidtest4() {
38+
tree =newint[]{3,3,3,1,2,1,1,2,3,3,4};
39+
assertEquals(5,solution1.totalFruit(tree));
40+
}
41+
42+
@Test
43+
publicvoidtest5() {
44+
tree =newint[]{0,1,6,6,4,4,6};
45+
assertEquals(5,solution1.totalFruit(tree));
46+
}
47+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp