Skip to content

Commit 0ac2c24

Browse files
authored
Merge pull request #1275 from 0xff-dev/2561
Add solution and test-cases for problem 2561
2 parents fa67119 + a10945b commit 0ac2c24

File tree

3 files changed

+93
-13
lines changed

3 files changed

+93
-13
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [2561.Rearranging Fruits][title]
2+
3+
## Description
4+
5+
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:
6+
7+
- Chose two indices `i` and `j`, and swap the `ith` fruit of `basket1` with the `jth` fruit of `basket2`.
8+
- The cost of the swap is `min(basket1[i],basket2[j])`.
9+
10+
Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.
11+
12+
Return the minimum cost to make both the baskets equal or `-1` if impossible.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: basket1 = [4,2,2,2], basket2 = [1,4,1,2]
18+
Output: 1
19+
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.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: basket1 = [2,3,4,1], basket2 = [3,2,5,1]
26+
Output: -1
27+
Explanation: It can be shown that it is impossible to make both the baskets equal.
28+
```
29+
30+
## 结语
31+
32+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
33+
34+
[title]: https://leetcode.com/problems/rearranging-fruits/
35+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
func Solution(basket1 []int, basket2 []int) int64 {
9+
freq := map[int]int{}
10+
m := math.MaxInt
11+
for _, b := range basket1 {
12+
freq[b]++
13+
if b < m {
14+
m = b
15+
}
16+
}
17+
for _, b := range basket2 {
18+
freq[b]--
19+
if b < m {
20+
m = b
21+
}
22+
}
23+
24+
var merge []int
25+
for k, c := range freq {
26+
if c%2 != 0 {
27+
return -1
28+
}
29+
for i := 0; i < abs(c)/2; i++ {
30+
merge = append(merge, k)
31+
}
32+
}
33+
34+
sort.Ints(merge)
35+
var res int64
36+
for i := 0; i < len(merge)/2; i++ {
37+
if 2*m < merge[i] {
38+
res += int64(2 * m)
39+
} else {
40+
res += int64(merge[i])
41+
}
42+
}
43+
return res
44+
}
45+
46+
func abs(a int) int {
47+
if a < 0 {
48+
return -a
49+
}
50+
return a
551
}

leetcode/2501-2600/2561.Rearranging-Fruits/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
basket1, basket2 []int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{4, 2, 2, 2}, []int{1, 4, 1, 2}, 1},
17+
{"TestCase2", []int{2, 3, 4, 1}, []int{1, 2, 5, 1}, -1},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.basket1, c.basket2)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.basket1, c.basket2)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)