Skip to content

Commit e17301e

Browse files
authored
update: 添加问题“904.水果成篮”的代码(并更新其题解) (#1066)
* 3202: O(k)空间 - AC.cpp+py (#1035) (#1041) cpp - AC,44.38%,64.27% cpp(no max) - AC,46.19%,67.47% py(no max - AC,61.42%,81.68% * word: en 2025.07.22 (#1035) (#1041) * 3202: O(k)空间 - AC.go+docs (#1035) (#1041) go - AC,17.49%,80.21% * 3202: docs-题解 (#1035) (#1041) * MGJW: git pull (#1046) - by others .pem泄露无所谓 反正为了指标的HTTPS只是一个笑话 * MGJW: test 150 times (#1046) 研二这个点加班的就我一个 - 这绝对有问题 * MGJW: once then once (#1046) 完成一个需求就会有下一个需求 * docs: 毕设月报 (#1046) 毁灭吧 世界 * MGJW: webex (#1046) 还挺快 * MGJW: git pull (#1046) by hand ofcoursely * MGJW: owa beacon generator (#1046) 看吧 我就说还有 * docs: 毕设月报 (#1046) 吃饭去 * log: 快快快 #1046 * docs: 毕设月报+en words (#1046) * 904: AC.cpp+WA.py (#1062) cpp - AC,60.78%,59.78% * archive: git switch master还没成功之时Ctrl+C了,然后pull master冲突了 Signed-off-by: LetMeFly666 <[email protected]> * feat: subprocess test - windows上也会主程序立刻结束运行但5秒后文件被追加 (#1062) If you are sure you want to delete it, run 'git branch -D dev'. PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> git push fatal: The current branch 904 has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin 904 To have this happen automatically for branches without a tracking upstream, see 'push.autoSetupRemote' in 'git help config'. PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> git push --set-upstream origin 904 Enumerating objects: 131, done. Counting objects: 100% (127/127), done. Delta compression using up to 8 threads Compressing objects: 100% (91/91), done. Writing objects: 100% (94/94), 70.89 KiB | 448.00 KiB/s, done. Total 94 (delta 74), reused 1 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (74/74), completed with 20 local objects. To ssh.github.com:LetMeFly666/LeetCode 9515dbd..48ec15d 904 -> 904 branch '904' set up to track 'origin/904'. PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> git branch -d dev warning: not deleting branch 'dev' that is not yet merged to 'refs/remotes/origin/dev', even though it is merged to HEAD. error: The branch 'dev' is not fully merged. If you are sure you want to delete it, run 'git branch -D dev'. PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> git branch -D Dev Deleted branch Dev (was b28f785). PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> * feat: subprocess test - windows上也会主程序立刻结束运行但5秒后文件被追加 (#1062) 48ec15d * 904: AC.py+go+WA.JAVA (#1062) py - AC,34.00%,55.14% go - AC,43.95%,98.14% * 904: AC.JAVA (#1062) java - AC,54.22%,11.58% * word: en words yesterday(2025.08.06) (#1062) * 904: CE.rust (#1062) * 904: CE.rust (#1062) * 904: AC.rist (#1062) rust - AC,46.96%,54.14% * word: en words today (#1062) * feat: rust-analyzer —— 一个不是很优雅的解决方案 (#1062) * feat: rust-analyzer (#1062) * 904: AC.rust (#1062) rust - AC,55.98%,57.61% * feat: rust-analyzer (humane) (#1062) * update: 添加问题“904.水果成篮”的代码(并更新其题解) (#1062) * feat: rust-analyzer (humane) (#1062) --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent d98ccff commit e17301e

18 files changed

+339
-44
lines changed

Codes/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# rust output
2+
target/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-05 10:28:59
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-05 10:31:43
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int totalFruit(vector<int>& fruits) {
14+
int ans = 0;
15+
unordered_map<int, int> window;
16+
for (int l = 0, r = 0; r < fruits.size(); r++) {
17+
window[fruits[r]]++;
18+
while (window.size() > 2) {
19+
if (! --window[fruits[l]]) {
20+
window.erase(fruits[l]);
21+
}
22+
l++;
23+
}
24+
ans = max(ans, r - l + 1);
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-05 10:28:59
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-06 18:33:14
6+
*/
7+
package main
8+
9+
func totalFruit(fruits []int) (ans int) {
10+
window := map[int]int{}
11+
for l, r := 0, 0; r < len(fruits); r++ {
12+
window[fruits[r]]++
13+
for len(window) > 2 {
14+
window[fruits[l]]--
15+
if window[fruits[l]] == 0 {
16+
delete(window, fruits[l])
17+
}
18+
l++
19+
}
20+
ans = max(ans, r - l + 1)
21+
}
22+
return
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-05 10:28:59
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-06 18:49:58
6+
*/
7+
import java.util.Map;
8+
import java.util.HashMap;
9+
10+
class Solution {
11+
public int totalFruit(int[] fruits) {
12+
int ans = 0;
13+
Map<Integer, Integer> window = new HashMap<>();
14+
for (int l = 0, r = 0; r < fruits.length; r++) {
15+
window.merge(fruits[r], 1, Integer::sum);
16+
while (window.size() > 2) {
17+
window.merge(fruits[l], -1, Integer::sum);
18+
if (window.get(fruits[l]) == 0) {
19+
window.remove(fruits[l]);
20+
}
21+
l++;
22+
}
23+
ans = Math.max(ans, r - l + 1);
24+
}
25+
return ans;
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-08-05 10:28:59
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-08-05 10:40:16
6+
'''
7+
from typing import List
8+
from collections import defaultdict
9+
10+
class Solution:
11+
def totalFruit(self, fruits: List[int]) -> int:
12+
ans = l = 0
13+
window = defaultdict(int)
14+
for r, v in enumerate(fruits):
15+
window[v] += 1
16+
while len(window) > 2:
17+
window[fruits[l]] -= 1
18+
if not window[fruits[l]]:
19+
del window[fruits[l]]
20+
l += 1
21+
ans = max(ans, r - l + 1)
22+
return ans
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-05 10:28:59
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-08 10:23:47
6+
*/
7+
use std::collections::HashMap;
8+
9+
impl Solution {
10+
pub fn total_fruit(fruits: Vec<i32>) -> i32 {
11+
let mut ans = 0;
12+
let mut l = 0;
13+
let mut window = HashMap::new();
14+
for (r, &x) in fruits.iter().enumerate() {
15+
*window.entry(x).or_insert(0) += 1;
16+
while window.len() > 2 {
17+
*window.entry(fruits[l]).or_insert(0) -= 1; // 即使键一定存在也需要or_insert(一个默认值)
18+
if window[&fruits[l]] == 0 { // 键必须是&i32
19+
window.remove(&fruits[l]);
20+
}
21+
l += 1;
22+
}
23+
ans = ans.max(r - l + 1);
24+
}
25+
ans as i32
26+
}
27+
}

Codes/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Codes/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!--
22
* @Author: LetMeFly
33
* @Date: 2023-10-20 22:24:18
4-
* @LastEditors: LetMeFly
5-
* @LastEditTime: 2023-10-20 22:24:18
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-08 09:52:46
66
-->
77
## AC代码
88

@@ -73,3 +73,7 @@ int main() {
7373
```
7474

7575
可以通过[Github Gist](https://gist.github.com/LetMeFly666/9e9c47db429532b212537cc1f6f36a42)进行下载/查看
76+
77+
## 其他文件
78+
79+
`cargo.lock``cargo.toml``lib.rs`都是rust所需

Codes/cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "LetMeFly-LeetCode-Codes"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# This tells rust-analyzer that the root of the library is lib.rs
7+
[lib]
8+
path = "lib.rs"
9+
10+
[dependencies]
11+
# Add any common dependencies here if you suspect they are used.
12+
# For now, we can leave it empty or add a few common ones for LeetCode.
13+
# e.g., itertools = "0.10"

Codes/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-07 14:11:36
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-08 10:19:31
6+
*/
7+
pub struct Solution;
8+
include!("0904-fruit-into-baskets_20250805.rs"); // 这个fileName是会被脚本替换掉的

0 commit comments

Comments
 (0)