Skip to content

Commit 5c7ab49

Browse files
committed
一刷904
1 parent 2d06b45 commit 5c7ab49

File tree

6 files changed

+64
-12
lines changed

6 files changed

+64
-12
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6353,14 +6353,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
63536353
//|{doc_base_url}/0903-valid-permutations-for-di-sequence.adoc[题解]
63546354
//|Hard
63556355
//|
6356-
//
6357-
//|{counter:codes}
6358-
//|{leetcode_base_url}/fruit-into-baskets/[904. Fruit Into Baskets^]
6359-
//|{source_base_url}/_0904_FruitIntoBaskets.java[Java]
6360-
//|{doc_base_url}/0904-fruit-into-baskets.adoc[题解]
6361-
//|Medium
6362-
//|
6363-
//
6356+
6357+
|{counter:codes}
6358+
|{leetcode_base_url}/fruit-into-baskets/[904. Fruit Into Baskets^]
6359+
|{source_base_url}/_0904_FruitIntoBaskets.java[Java]
6360+
|{doc_base_url}/0904-fruit-into-baskets.adoc[题解]
6361+
|Medium
6362+
|
6363+
63646364
//|{counter:codes}
63656365
//|{leetcode_base_url}/sort-array-by-parity/[905. Sort Array By Parity^]
63666366
//|{source_base_url}/_0905_SortArrayByParity.java[Java]

docs/0000-08-sliding-window.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,4 @@ while (right < s.size()) {
8383
. https://mp.weixin.qq.com/s/6YeZUCYj5ft-OGa85sQegw[面试官,你再问我滑动窗口问题试试?我有解题模板,不怕!^]
8484
. https://www.bilibili.com/video/BV1hd4y1r7Gq/[滑动窗口【基础算法精讲 03】^]
8585
. https://leetcode.cn/problems/subarray-product-less-than-k/solutions/1320871/jian-dan-yi-dong-xiang-xi-zhu-jie-shuang-jvy3/[713. 乘积小于 K 的子数组 - 官方思路秒懂○注释详细○双指针滑窗 【附通用滑窗模板】^]
86+
. https://leetcode.cn/problems/fruit-into-baskets/solutions/1437444/shen-du-jie-xi-zhe-dao-ti-he-by-linzeyin-6crr/[904. 水果成篮 - 【深度解析】这道题和76. 最小覆盖子串的区别^] -- 这个题解比较了“最大滑动窗口”和“最小滑动窗口”的区别。可以研究一下。

docs/0904-fruit-into-baskets.adoc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ If we started at the first tree or the eighth tree, we would only collect 4 frui
7070
*Note:*
7171

7272

73-
. `1 <= tree.length <= 40000`
74-
. `0 <= tree[i] < tree.length`
75-
73+
. `1 \<= tree.length \<= 40000`
74+
. `0 \<= tree[i] \< tree.length`
7675

76+
== 思路分析
7777

7878

7979
[[src-0904]]
@@ -82,3 +82,9 @@ If we started at the first tree or the eighth tree, we would only collect 4 frui
8282
include::{sourcedir}/_0904_FruitIntoBaskets.java[tag=answer]
8383
----
8484

85+
== 参考资料
86+
87+
. https://leetcode.cn/problems/fruit-into-baskets/solutions/1893352/shui-guo-cheng-lan-by-leetcode-solution-1uyu/[904. 水果成篮 - 官方题解^]
88+
. https://leetcode.cn/problems/fruit-into-baskets/solutions/1437444/shen-du-jie-xi-zhe-dao-ti-he-by-linzeyin-6crr/[904. 水果成篮 - 【深度解析】这道题和76. 最小覆盖子串的区别^]
89+
. https://leetcode.cn/problems/fruit-into-baskets/solutions/1897490/by-ac_oier-skgk/[904. 水果成篮 - 【宫水三叶】简单滑动窗口运用题^]
90+

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1882,7 +1882,7 @@ include::0892-surface-area-of-3d-shapes.adoc[leveloffset=+1]
18821882

18831883
// include::0903-valid-permutations-for-di-sequence.adoc[leveloffset=+1]
18841884

1885-
// include::0904-fruit-into-baskets.adoc[leveloffset=+1]
1885+
include::0904-fruit-into-baskets.adoc[leveloffset=+1]
18861886

18871887
// include::0905-sort-array-by-parity.adoc[leveloffset=+1]
18881888

logbook/202406.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,12 @@
893893
|{doc_base_url}/1110-delete-nodes-and-return-forest.adoc[题解]
894894
|✅ 递归+深度优先
895895

896+
|{counter:codes}
897+
|{leetcode_base_url}/fruit-into-baskets/[904. Fruit Into Baskets^]
898+
|{doc_base_url}/0904-fruit-into-baskets.adoc[题解]
899+
|✅ 滑动窗口
900+
901+
896902
|===
897903

898904
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _0904_FruitIntoBaskets {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2024-09-27 15:33:58
11+
*/
12+
public int totalFruit(int[] fruits) {
13+
int result = 0;
14+
int head = 0, tail = 0;
15+
Map<Integer, Integer> fruitToCntMap = new HashMap<>();
16+
while (head < fruits.length) {
17+
int f1 = fruits[head];
18+
head++;
19+
Integer cnt = fruitToCntMap.getOrDefault(f1, 0);
20+
fruitToCntMap.put(f1, cnt + 1);
21+
if (fruitToCntMap.size() <= 2) {
22+
result = Math.max(result, head - tail);
23+
} else {
24+
while (tail < head && fruitToCntMap.size() > 2) {
25+
int f2 = fruits[tail];
26+
Integer c2 = fruitToCntMap.get(f2);
27+
if (c2 >1) {
28+
fruitToCntMap.put(f2, c2 - 1);
29+
}else {
30+
fruitToCntMap.remove(f2);
31+
}
32+
tail++;
33+
}
34+
}
35+
}
36+
return result;
37+
}
38+
// end::answer[]
39+
}

0 commit comments

Comments
 (0)