Skip to content

Commit bbe24c7

Browse files
authored
feat: add solutions to lc problem: No.0904 (#4620)
No.0904.Fruit Into Baskets
1 parent 68291ca commit bbe24c7

File tree

4 files changed

+176
-9
lines changed

4 files changed

+176
-9
lines changed

solution/0900-0999/0904.Fruit Into Baskets/README.md

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ tags:
8383

8484
### 方法一:哈希表 + 滑动窗口
8585

86-
我们用哈希表 $cnt$ 维护当前窗口内的水果种类以及对应的数量,用双指针 $j$ 和 $i$ 维护窗口的左右边界。
86+
我们用哈希表 $\textit{cnt}$ 维护当前窗口内的水果种类以及对应的数量,用双指针 $j$ 和 $i$ 维护窗口的左右边界。
8787

88-
遍历数组 `fruits`,将当前水果 $x$ 加入窗口,即 $cnt[x]++$,然后判断当前窗口内的水果种类是否超过了 $2$ 种,如果超过了 $2$ 种,就需要将窗口的左边界 $j$ 右移,直到窗口内的水果种类不超过 $2$ 种为止。然后更新答案,即 $ans = \max(ans, i - j + 1)$。
88+
遍历数组 $\textit{fruits}$,将当前水果 $x$ 加入窗口,即 $\textit{cnt}[x]++$,然后判断当前窗口内的水果种类是否超过了 $2$ 种,如果超过了 $2$ 种,就需要将窗口的左边界 $j$ 右移,直到窗口内的水果种类不超过 $2$ 种为止。然后更新答案,即 $\textit{ans} = \max(\textit{ans}, i - j + 1)$。
8989

9090
遍历结束后,即可得到最终的答案。
9191

@@ -105,7 +105,7 @@ j i
105105
j i
106106
```
107107

108-
时间复杂度 $O(n)$,其中 $n$ 为数组 `fruits` 的长度。空间复杂度 $O(1)$。
108+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{fruits}$ 的长度。空间复杂度 $O(1)$,因为哈希表 $\textit{cnt}$ 中的键值对数量最多为 $2$。
109109

110110
<!-- tabs:start -->
111111

@@ -248,19 +248,49 @@ impl Solution {
248248
}
249249
```
250250

251+
#### C#
252+
253+
```cs
254+
public class Solution {
255+
public int TotalFruit(int[] fruits) {
256+
var cnt = new Dictionary<int, int>();
257+
int ans = 0;
258+
for (int i = 0, j = 0; i < fruits.Length; ++i) {
259+
int x = fruits[i];
260+
if (cnt.ContainsKey(x)) {
261+
cnt[x]++;
262+
} else {
263+
cnt[x] = 1;
264+
}
265+
while (cnt.Count > 2) {
266+
int y = fruits[j++];
267+
if (cnt.ContainsKey(y)) {
268+
cnt[y]--;
269+
if (cnt[y] == 0) {
270+
cnt.Remove(y);
271+
}
272+
}
273+
}
274+
ans = Math.Max(ans, i - j + 1);
275+
}
276+
return ans;
277+
}
278+
}
279+
```
280+
251281
<!-- tabs:end -->
252282

253283
<!-- solution:end -->
254284

255285
<!-- solution:start -->
256286

257-
### 方法二:滑动窗口优化
287+
### 方法二:单调变长滑动窗口
258288

259289
在方法一中,我们发现,窗口大小会时而变大,时而变小,这就需要我们每一次更新答案。
260290

261291
但本题实际上求的是水果的最大数目,也就是“最大”的窗口,我们没有必要缩小窗口,只需要让窗口单调增大。于是代码就少了每次更新答案的操作,只需要在遍历结束后将此时的窗口大小作为答案返回即可。
262292

263-
时间复杂度 $O(n)$,其中 $n$ 为数组 `fruits` 的长度。空间复杂度 $O(1)$
293+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{fruits}$ 的长度。
264294

265295
<!-- tabs:start -->
266296

@@ -395,6 +425,35 @@ impl Solution {
395425
}
396426
```
397427

428+
#### C#
429+
430+
```cs
431+
public class Solution {
432+
public int TotalFruit(int[] fruits) {
433+
var cnt = new Dictionary<int, int>();
434+
int j = 0, n = fruits.Length;
435+
foreach (int x in fruits) {
436+
if (cnt.ContainsKey(x)) {
437+
cnt[x]++;
438+
} else {
439+
cnt[x] = 1;
440+
}
441+
442+
if (cnt.Count > 2) {
443+
int y = fruits[j++];
444+
if (cnt.ContainsKey(y)) {
445+
cnt[y]--;
446+
if (cnt[y] == 0) {
447+
cnt.Remove(y);
448+
}
449+
}
450+
}
451+
}
452+
return n - j;
453+
}
454+
}
455+
```
456+
398457
<!-- tabs:end -->
399458

400459
<!-- solution:end -->

solution/0900-0999/0904.Fruit Into Baskets/README_EN.md

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ If we had started at the first tree, we would only pick from trees [1,2].
7575

7676
We use a hash table $cnt$ to maintain the types and corresponding quantities of fruits in the current window, and use two pointers $j$ and $i$ to maintain the left and right boundaries of the window.
7777

78-
We traverse the `fruits` array, add the current fruit $x$ to the window, i.e., $cnt[x]++$, then judge whether the types of fruits in the current window exceed $2$. If it exceeds $2$, we need to move the left boundary $j$ of the window to the right until the types of fruits in the window do not exceed $2$. Then we update the answer, i.e., $ans = \max(ans, i - j + 1)$.
78+
We traverse the $\textit{fruits}$ array, add the current fruit $x$ to the window, i.e., $cnt[x]++$, then judge whether the types of fruits in the current window exceed $2$. If it exceeds $2$, we need to move the left boundary $j$ of the window to the right until the types of fruits in the window do not exceed $2$. Then we update the answer, i.e., $ans = \max(ans, i - j + 1)$.
7979

8080
After the traversal ends, we can get the final answer.
8181

@@ -95,7 +95,7 @@ j i
9595
j i
9696
```
9797

98-
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the `fruits` array.
98+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the $\textit{fruits}$ array.
9999

100100
<!-- tabs:start -->
101101

@@ -238,19 +238,49 @@ impl Solution {
238238
}
239239
```
240240

241+
#### C#
242+
243+
```cs
244+
public class Solution {
245+
public int TotalFruit(int[] fruits) {
246+
var cnt = new Dictionary<int, int>();
247+
int ans = 0;
248+
for (int i = 0, j = 0; i < fruits.Length; ++i) {
249+
int x = fruits[i];
250+
if (cnt.ContainsKey(x)) {
251+
cnt[x]++;
252+
} else {
253+
cnt[x] = 1;
254+
}
255+
while (cnt.Count > 2) {
256+
int y = fruits[j++];
257+
if (cnt.ContainsKey(y)) {
258+
cnt[y]--;
259+
if (cnt[y] == 0) {
260+
cnt.Remove(y);
261+
}
262+
}
263+
}
264+
ans = Math.Max(ans, i - j + 1);
265+
}
266+
return ans;
267+
}
268+
}
269+
```
270+
241271
<!-- tabs:end -->
242272

243273
<!-- solution:end -->
244274

245275
<!-- solution:start -->
246276

247-
### Solution 2: Sliding Window Optimization
277+
### Solution 2: Monotonic Variable-Length Sliding Window
248278

249279
In Solution 1, we find that the window size sometimes increases and sometimes decreases, which requires us to update the answer each time.
250280

251281
But what this problem actually asks for is the maximum number of fruits, that is, the "largest" window. We don't need to shrink the window, we just need to let the window monotonically increase. So the code omits the operation of updating the answer each time, and only needs to return the size of the window as the answer after the traversal ends.
252282

253-
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the `fruits` array.
283+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the $\textit{fruits}$ array.
254284

255285
<!-- tabs:start -->
256286

@@ -385,6 +415,35 @@ impl Solution {
385415
}
386416
```
387417

418+
#### C#
419+
420+
```cs
421+
public class Solution {
422+
public int TotalFruit(int[] fruits) {
423+
var cnt = new Dictionary<int, int>();
424+
int j = 0, n = fruits.Length;
425+
foreach (int x in fruits) {
426+
if (cnt.ContainsKey(x)) {
427+
cnt[x]++;
428+
} else {
429+
cnt[x] = 1;
430+
}
431+
432+
if (cnt.Count > 2) {
433+
int y = fruits[j++];
434+
if (cnt.ContainsKey(y)) {
435+
cnt[y]--;
436+
if (cnt[y] == 0) {
437+
cnt.Remove(y);
438+
}
439+
}
440+
}
441+
}
442+
return n - j;
443+
}
444+
}
445+
```
446+
388447
<!-- tabs:end -->
389448

390449
<!-- solution:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public int TotalFruit(int[] fruits) {
3+
var cnt = new Dictionary<int, int>();
4+
int ans = 0;
5+
for (int i = 0, j = 0; i < fruits.Length; ++i) {
6+
int x = fruits[i];
7+
if (cnt.ContainsKey(x)) {
8+
cnt[x]++;
9+
} else {
10+
cnt[x] = 1;
11+
}
12+
while (cnt.Count > 2) {
13+
int y = fruits[j++];
14+
if (cnt.ContainsKey(y)) {
15+
cnt[y]--;
16+
if (cnt[y] == 0) {
17+
cnt.Remove(y);
18+
}
19+
}
20+
}
21+
ans = Math.Max(ans, i - j + 1);
22+
}
23+
return ans;
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
public int TotalFruit(int[] fruits) {
3+
var cnt = new Dictionary<int, int>();
4+
int j = 0, n = fruits.Length;
5+
foreach (int x in fruits) {
6+
if (cnt.ContainsKey(x)) {
7+
cnt[x]++;
8+
} else {
9+
cnt[x] = 1;
10+
}
11+
12+
if (cnt.Count > 2) {
13+
int y = fruits[j++];
14+
if (cnt.ContainsKey(y)) {
15+
cnt[y]--;
16+
if (cnt[y] == 0) {
17+
cnt.Remove(y);
18+
}
19+
}
20+
}
21+
}
22+
return n - j;
23+
}
24+
}

0 commit comments

Comments
 (0)