Skip to content

Add solution and test-cases for problem 2561 #1275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions leetcode/2501-2600/2561.Rearranging-Fruits/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# [2561.Rearranging Fruits][title]

## Description

You have two fruit baskets containing `n` fruits each. You are given two **0-indexed** integer arrays `basket1` and `basket2` representing the cost of fruit in each basket. You want to make both baskets **equal**. To do so, you can use the following operation as many times as you want:

- Chose two indices `i` and `j`, and swap the `ith` fruit of `basket1` with the `jth` fruit of `basket2`.
- The cost of the swap is `min(basket1[i],basket2[j])`.

Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.

Return the minimum cost to make both the baskets equal or `-1` if impossible.

**Example 1:**

```
Input: basket1 = [4,2,2,2], basket2 = [1,4,1,2]
Output: 1
Explanation: Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal.
```

**Example 2:**

```
Input: basket1 = [2,3,4,1], basket2 = [3,2,5,1]
Output: -1
Explanation: It can be shown that it is impossible to make both the baskets equal.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/rearranging-fruits/
[me]: https://github.com/kylesliu/awesome-golang-algorithm
50 changes: 48 additions & 2 deletions leetcode/2501-2600/2561.Rearranging-Fruits/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
package Solution

func Solution(x bool) bool {
return x
import (
"math"
"sort"
)

func Solution(basket1 []int, basket2 []int) int64 {
freq := map[int]int{}
m := math.MaxInt
for _, b := range basket1 {
freq[b]++
if b < m {
m = b
}
}
for _, b := range basket2 {
freq[b]--
if b < m {
m = b
}
}

var merge []int
for k, c := range freq {
if c%2 != 0 {
return -1
}
for i := 0; i < abs(c)/2; i++ {
merge = append(merge, k)
}
}

sort.Ints(merge)
var res int64
for i := 0; i < len(merge)/2; i++ {
if 2*m < merge[i] {
res += int64(2 * m)
} else {
res += int64(merge[i])
}
}
return res
}

func abs(a int) int {
if a < 0 {
return -a
}
return a
}
21 changes: 10 additions & 11 deletions leetcode/2501-2600/2561.Rearranging-Fruits/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
basket1, basket2 []int
expect int64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{4, 2, 2, 2}, []int{1, 4, 1, 2}, 1},
{"TestCase2", []int{2, 3, 4, 1}, []int{1, 2, 5, 1}, -1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.basket1, c.basket2)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.basket1, c.basket2)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading