From e013cda898b768546c48dce6936470d49d0bc73d Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 12 Dec 2024 09:10:25 +0800 Subject: [PATCH] Add solution and test-cases for problem 2558 --- .../README.md | 42 ++++++++++++++ .../Solution.go | 57 ++++++++++++++++++- .../Solution_test.go | 20 +++---- 3 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/README.md diff --git a/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/README.md b/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/README.md new file mode 100644 index 00000000..fa3173bf --- /dev/null +++ b/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/README.md @@ -0,0 +1,42 @@ +# [2558.Take Gifts From the Richest Pile][title] + +## Description +You are given an integer array `gifts` denoting the number of gifts in various piles. Every second, you do the following: + +- Choose the pile with the maximum number of gifts. +- If there is more than one pile with the maximum number of gifts, choose any. +- Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts. + +Return the number of gifts remaining after `k` seconds. + +**Example 1:** + +``` +Input: gifts = [25,64,9,4,100], k = 4 +Output: 29 +Explanation: +The gifts are taken in the following way: +- In the first second, the last pile is chosen and 10 gifts are left behind. +- Then the second pile is chosen and 8 gifts are left behind. +- After that the first pile is chosen and 5 gifts are left behind. +- Finally, the last pile is chosen again and 3 gifts are left behind. +The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29. +``` + +**Example 2:** + +``` +Input: gifts = [1,1,1,1], k = 4 +Output: 4 +Explanation: +In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile. +That is, you can't take any pile with you. +So, the total gifts remaining are 4. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/take-gifts-from-the-richest-pile +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/Solution.go b/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/Solution.go index d115ccf5..18aa7a83 100755 --- a/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/Solution.go +++ b/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/Solution.go @@ -1,5 +1,60 @@ package Solution -func Solution(x bool) bool { +import "container/heap" + +type heap2558 []int + +func (h *heap2558) Len() int { + return len(*h) +} + +func (h *heap2558) Swap(i, j int) { + (*h)[i], (*h)[j] = (*h)[j], (*h)[i] +} + +func (h *heap2558) Less(i, j int) bool { + return (*h)[i] > (*h)[j] +} + +func (h *heap2558) Push(x any) { + *h = append(*h, x.(int)) +} + +func (h *heap2558) Pop() any { + old := *h + l := len(old) + x := old[l-1] + *h = old[:l-1] return x } + +func Sqrt2558(x int) int { + if x == 0 { + return 0 + } + + r := x + for r*r > x { + r = (r + x/r) / 2 + } + + return r +} + +func Solution(gifts []int, k int) int64 { + ans := int64(0) + h := &heap2558{} + for _, n := range gifts { + heap.Push(h, n) + } + for ; k > 0; k-- { + top := heap.Pop(h).(int) + x := Sqrt2558(top) + heap.Push(h, x) + } + for h.Len() > 0 { + top := heap.Pop(h).(int) + ans += int64(top) + } + return ans +} diff --git a/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/Solution_test.go b/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/Solution_test.go index 14ff50eb..c7de27ad 100755 --- a/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/Solution_test.go +++ b/leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + gifts []int + k int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{25, 64, 9, 4, 100}, 4, 29}, + {"TestCase2", []int{1, 1, 1, 1}, 4, 4}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.gifts, c.k) 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.gifts, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }