Skip to content

Commit

Permalink
Merge pull request #1061 from 0xff-dev/2558
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2558
  • Loading branch information
6boris authored Dec 24, 2024
2 parents 8ac25eb + e013cda commit 4fe59ad
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 11 deletions.
42 changes: 42 additions & 0 deletions leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/README.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

0 comments on commit 4fe59ad

Please sign in to comment.