Skip to content

Commit d98ccff

Browse files
authored
update: 添加问题“2561.重排水果”的代码和题解 (#1060)
* 2561: WA.cpp (#1059) * 2561: RE.cpp (#1059) - 爆int了 * 2561: AC.cpp (#1059) * 2561: WA.py (#1059) * 2561: AC.py (#1059) cpp - AC,37.63%,96.77% py - AC,62.59%,60.43% * word(2561): WA.go (#1059) 不要被trae的自动补全所干扰 * word(2561): RE.go (#1059) Line 31: Char 25: undefined: abs (solution.go) * 2561: AC.go+CE.java (#1059) go - AC,36.54%,26.25% Line 17: error: method merge in interface Map<K,V> cannot be applied to given types; timeDiffs.merge(1, Integer::sum); ^ required: Integer,Integer,BiFunction<? super Integer,? super Integer,? extends Integer> found: int,Integer::sum reason: actual and formal argument lists differ in length where K,V are type-variables: K extends Object declared in interface Map V extends Object declared in interface Map Line 17: error: incompatible types: Integer is not a functional interface timeDiffs.merge(1, Integer::sum); ^ Line 21: error: method merge in interface Map<K,V> cannot be applied to given types; timeDiffs.merge(-1, Integer::sum); ^ required: Integer,Integer,BiFunction<? super Integer,? super Integer,? extends Integer> found: int,Integer::sum reason: actual and formal argument lists differ in length where K,V are type-variables: K extends Object declared in interface Map V extends Object declared in interface Map Line 21: error: incompatible types: Integer is not a functional interface timeDiffs.merge(-1, Integer::sum); ^ Line 39: error: method sort in interface List<E> cannot be applied to given types; change.sort(); ^ required: Comparator<? super Integer> found: no arguments reason: actual and formal argument lists differ in length where E is a type-variable: E extends Object declared in interface List 5 errors * 2561: AC.java (#1059) - 小憩一会儿 java - AC,94.93%,97.21% * 2561: CE.rust (#1059) * 2561: CE.rust (#1059) 困了说明,也太不走心了 * 2561: AC.rust (#1059) rust - AC,34.69%,63.27% * update: 添加问题“2561.重排水果”的代码和题解 (#1060) Signed-off-by: LetMeFly666 <[email protected]> * typo: 哈哈 leetcode还干过这事儿呢 (#1059) --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent ec954cb commit d98ccff

15 files changed

+590
-5
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
"request": "launch",
3939
"program": "${fileDirname}/${fileBasenameNoExtension}",
4040
"args": [],
41-
"stopAtEntry": false,
41+
"stopAtEntry": true,
4242
"cwd": "${fileDirname}",
4343
"environment": [],
44-
"externalConsole": false,
44+
"externalConsole": true,
4545
"MIMode": "lldb",
4646
"preLaunchTask": "C/C++: clang++ 生成活动文件"
4747
}

AllProblems/2561.重排水果/2561.重排水果.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ tags: [题解, LeetCode, 困难, 贪心, 数组, 哈希表]
4242
<p><strong>提示:</strong></p>
4343

4444
<ul>
45-
<li><code>basket1.length == bakste2.length</code></li>
45+
<li><code>basket1.length == bakste2.length</code></li> typo 哈哈 leetcode还干过这事儿呢 - 等下次执行脚本覆盖掉吧
4646
<li><code>1 &lt;= basket1.length &lt;= 10<sup>5</sup></code></li>
4747
<li><code>1 &lt;= basket1<sub>i</sub>,basket2<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
4848
</ul>

Codes/2561-rearranging-fruits.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-03 22:53:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-03 23:46:56
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/*
12+
假设要交换的有 3 4 30 40
13+
14+
b1: [3, 30]
15+
b2: [4, 40]
16+
17+
则min: 3+4
18+
19+
*/
20+
typedef long long ll;
21+
class Solution {
22+
public:
23+
long long minCost(vector<int>& basket1, vector<int>& basket2) {
24+
unordered_map<int, int> diffTimes;
25+
int m = INT_MAX;
26+
for (int t : basket1) {
27+
diffTimes[t]++;
28+
m = min(m, t);
29+
}
30+
for (int t : basket2) {
31+
diffTimes[t]--;
32+
m = min(m, t);
33+
}
34+
vector<int> change;
35+
for (auto [val, times] : diffTimes) {
36+
if (!times) {
37+
continue;
38+
}
39+
if (times % 2) {
40+
return -1;
41+
}
42+
for (int i = 0; i < abs(times) / 2; i++) {
43+
change.push_back(val);
44+
}
45+
}
46+
sort(change.begin(), change.end());
47+
ll ans = 0;
48+
for (int i = 0; i < change.size() / 2; i++) {
49+
ans += min(change[i], 2 * m);
50+
}
51+
return ans;
52+
}
53+
};
54+
55+
/*
56+
[4,2,2,2]
57+
[1,4,1,2]
58+
59+
1
60+
*/
61+
#if defined(_WIN32) || defined(__APPLE__)
62+
int main() {
63+
string s1, s2;
64+
while (cin >> s1 >> s2) {
65+
vector<int> v1 = stringToVector(s1), v2 = stringToVector(s2);
66+
Solution sol;
67+
cout << sol.minCost(v1, v2) << endl;
68+
}
69+
return 0;
70+
}
71+
#endif

Codes/2561-rearranging-fruits.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-03 22:53:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-04 13:38:44
6+
*/
7+
package main
8+
9+
import "sort"
10+
11+
func abs2561(x int) int {
12+
if x < 0 {
13+
return -x
14+
}
15+
return x
16+
}
17+
18+
func minCost(basket1 []int, basket2 []int) (ans int64) {
19+
timeDiffs := map[int]int{}
20+
m := 1000000000
21+
for _, t := range basket1 {
22+
timeDiffs[t]++
23+
m = min(m, t)
24+
}
25+
for _, t := range basket2 {
26+
timeDiffs[t]--
27+
m = min(m, t)
28+
}
29+
30+
change := []int{}
31+
for val, times := range timeDiffs {
32+
if times == 0 {
33+
continue
34+
}
35+
if times % 2 != 0 {
36+
return -1
37+
}
38+
for range abs2561(times) / 2 { // 刚发现go还有这种语法呢
39+
change = append(change, val)
40+
}
41+
}
42+
43+
sort.Ints(change)
44+
for i := 0; i < len(change) / 2; i++ {
45+
ans += int64(min(2 * m, change[i]))
46+
}
47+
return
48+
}

Codes/2561-rearranging-fruits.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-03 22:53:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-04 13:46:46
6+
*/
7+
import java.util.Map;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.ArrayList;
11+
12+
class Solution {
13+
public long minCost(int[] basket1, int[] basket2) {
14+
int m = basket1[0];
15+
Map<Integer, Integer> timeDiffs = new HashMap<>();
16+
for (int t : basket1) {
17+
timeDiffs.merge(t, 1, Integer::sum);
18+
m = Math.min(m, t);
19+
}
20+
for (int t : basket2) {
21+
timeDiffs.merge(t, -1, Integer::sum);
22+
m = Math.min(m, t);
23+
}
24+
25+
List<Integer> change = new ArrayList<>();
26+
for (Map.Entry<Integer, Integer> entry : timeDiffs.entrySet()) {
27+
if (entry.getValue() == 0) {
28+
continue;
29+
}
30+
if (entry.getValue() % 2 != 0) {
31+
return -1;
32+
}
33+
for (int i = 0; i < Math.abs(entry.getValue()) / 2; i++) {
34+
change.add(entry.getKey());
35+
}
36+
}
37+
38+
long ans = 0;
39+
change.sort(Integer::compareTo);
40+
for (int i = 0; i < change.size() / 2; i++) {
41+
ans += Math.min(2 * m, change.get(i));
42+
}
43+
return ans;
44+
}
45+
}

Codes/2561-rearranging-fruits.py

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-03 22:53:42
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-08-04 00:07:41
6+
'''
7+
from typing import List
8+
from collections import defaultdict
9+
10+
class Solution:
11+
def minCost(self, basket1: List[int], basket2: List[int]) -> int:
12+
m = min(min(basket1), min(basket2))
13+
diffTimes = defaultdict(int)
14+
for t in basket1:
15+
diffTimes[t] += 1
16+
for t in basket2:
17+
diffTimes[t] -= 1
18+
change = []
19+
for val, times in diffTimes.items():
20+
if not times:
21+
continue
22+
if times % 2:
23+
return -1
24+
for _ in range(abs(times) // 2):
25+
change.append(val)
26+
change.sort()
27+
return sum(min(m * 2, t) for t in change[:len(change) // 2])

Codes/2561-rearranging-fruits.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-08-03 22:53:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-08-04 23:06:33
6+
*/
7+
use std::collections::HashMap;
8+
9+
impl Solution {
10+
pub fn min_cost(basket1: Vec<i32>, basket2: Vec<i32>) -> i64 {
11+
let mut m: i32 = i32::MAX;
12+
let mut timeDiffs = HashMap::new();
13+
for &t in &basket1 {
14+
*timeDiffs.entry(t).or_insert(0) += 1;
15+
m = m.min(t);
16+
}
17+
for &t in &basket2 {
18+
*timeDiffs.entry(t).or_insert(0) -= 1;
19+
m = m.min(t);
20+
}
21+
22+
let mut change: Vec<i32> = vec![];
23+
for (&val, &times) in timeDiffs.iter() {
24+
if times % 2 != 0 {
25+
return -1;
26+
}
27+
for _ in 0..((times as i32).abs() / 2) {
28+
change.push(val);
29+
}
30+
}
31+
32+
change.sort_unstable();
33+
let mut ans: i64 = 0;
34+
for i in 0..(change.len() / 2) {
35+
ans += i64::from(change[i].min(2 * m));
36+
}
37+
ans
38+
}
39+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@
806806
|2555.两个线段获得的最多奖品|中等|<a href="https://leetcode.cn/problems/maximize-win-from-two-segments/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/09/11/LeetCode%202555.%E4%B8%A4%E4%B8%AA%E7%BA%BF%E6%AE%B5%E8%8E%B7%E5%BE%97%E7%9A%84%E6%9C%80%E5%A4%9A%E5%A5%96%E5%93%81/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142149662" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximize-win-from-two-segments/solutions/2913563/letmefly-2555liang-ge-xian-duan-huo-de-d-fq43/" target="_blank">LeetCode题解</a>|
807807
|2558.从数量最多的堆取走礼物|简单|<a href="https://leetcode.cn/problems/take-gifts-from-the-richest-pile/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/28/LeetCode%202558.%E4%BB%8E%E6%95%B0%E9%87%8F%E6%9C%80%E5%A4%9A%E7%9A%84%E5%A0%86%E5%8F%96%E8%B5%B0%E7%A4%BC%E7%89%A9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134088006" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/take-gifts-from-the-richest-pile/solutions/2501756/letmefly-2558cong-shu-liang-zui-duo-de-d-amgs/" target="_blank">LeetCode题解</a>|
808808
|2559.统计范围内的元音字符串数|中等|<a href="https://leetcode.cn/problems/count-vowel-strings-in-ranges/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/06/02/LeetCode%202559.%E7%BB%9F%E8%AE%A1%E8%8C%83%E5%9B%B4%E5%86%85%E7%9A%84%E5%85%83%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131014779" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-vowel-strings-in-ranges/solutions/2294361/letmefly-2559tong-ji-fan-wei-nei-de-yuan-mq4f/" target="_blank">LeetCode题解</a>|
809+
|2561.重排水果|困难|<a href="https://leetcode.cn/problems/rearranging-fruits/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/03/LeetCode%202561.%E9%87%8D%E6%8E%92%E6%B0%B4%E6%9E%9C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/149917019" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/rearranging-fruits/solutions/3742677/letmefly-2561zhong-pai-shui-guo-tan-xin-zsfq3/" target="_blank">LeetCode题解</a>|
809810
|2562.找出数组的串联值|简单|<a href="https://leetcode.cn/problems/find-the-array-concatenation-value/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/12/LeetCode%202562.%E6%89%BE%E5%87%BA%E6%95%B0%E7%BB%84%E7%9A%84%E4%B8%B2%E8%81%94%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133797249" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-array-concatenation-value/solutions/2479676/letmefly-2562zhao-chu-shu-zu-de-chuan-li-5atk/" target="_blank">LeetCode题解</a>|
810811
|2563.统计公平数对的数目|中等|<a href="https://leetcode.cn/problems/count-the-number-of-fair-pairs/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/19/LeetCode%202563.%E7%BB%9F%E8%AE%A1%E5%85%AC%E5%B9%B3%E6%95%B0%E5%AF%B9%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147354620" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-fair-pairs/solutions/3656121/letmefly-2563tong-ji-gong-ping-shu-dui-d-u9bf/" target="_blank">LeetCode题解</a>|
811812
|2575.找出字符串的可整除数组|中等|<a href="https://leetcode.cn/problems/find-the-divisibility-array-of-a-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/07/LeetCode%202575.%E6%89%BE%E5%87%BA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%8F%AF%E6%95%B4%E9%99%A4%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136544808" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-divisibility-array-of-a-string/solutions/2673564/letmefly-2575zhao-chu-zi-fu-chuan-de-ke-rbnfy/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)