diff --git a/leetcode/2501-2600/2561.Rearranging-Fruits/README.md b/leetcode/2501-2600/2561.Rearranging-Fruits/README.md new file mode 100644 index 000000000..b951710f8 --- /dev/null +++ b/leetcode/2501-2600/2561.Rearranging-Fruits/README.md @@ -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 diff --git a/leetcode/2501-2600/2561.Rearranging-Fruits/Solution.go b/leetcode/2501-2600/2561.Rearranging-Fruits/Solution.go index d115ccf5e..5a2fdacd1 100755 --- a/leetcode/2501-2600/2561.Rearranging-Fruits/Solution.go +++ b/leetcode/2501-2600/2561.Rearranging-Fruits/Solution.go @@ -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 } diff --git a/leetcode/2501-2600/2561.Rearranging-Fruits/Solution_test.go b/leetcode/2501-2600/2561.Rearranging-Fruits/Solution_test.go index 14ff50eb4..bb21c0fb3 100755 --- a/leetcode/2501-2600/2561.Rearranging-Fruits/Solution_test.go +++ b/leetcode/2501-2600/2561.Rearranging-Fruits/Solution_test.go @@ -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() { }