Skip to content

Commit 4fe59ad

Browse files
authored
Merge pull request #1061 from 0xff-dev/2558
Add solution and test-cases for problem 2558
2 parents 8ac25eb + e013cda commit 4fe59ad

File tree

3 files changed

+108
-11
lines changed

3 files changed

+108
-11
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [2558.Take Gifts From the Richest Pile][title]
2+
3+
## Description
4+
You are given an integer array `gifts` denoting the number of gifts in various piles. Every second, you do the following:
5+
6+
- Choose the pile with the maximum number of gifts.
7+
- If there is more than one pile with the maximum number of gifts, choose any.
8+
- Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.
9+
10+
Return the number of gifts remaining after `k` seconds.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: gifts = [25,64,9,4,100], k = 4
16+
Output: 29
17+
Explanation:
18+
The gifts are taken in the following way:
19+
- In the first second, the last pile is chosen and 10 gifts are left behind.
20+
- Then the second pile is chosen and 8 gifts are left behind.
21+
- After that the first pile is chosen and 5 gifts are left behind.
22+
- Finally, the last pile is chosen again and 3 gifts are left behind.
23+
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: gifts = [1,1,1,1], k = 4
30+
Output: 4
31+
Explanation:
32+
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
33+
That is, you can't take any pile with you.
34+
So, the total gifts remaining are 4.
35+
```
36+
37+
## 结语
38+
39+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
40+
41+
[title]: https://leetcode.com/problems/take-gifts-from-the-richest-pile
42+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,60 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
type heap2558 []int
6+
7+
func (h *heap2558) Len() int {
8+
return len(*h)
9+
}
10+
11+
func (h *heap2558) Swap(i, j int) {
12+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
13+
}
14+
15+
func (h *heap2558) Less(i, j int) bool {
16+
return (*h)[i] > (*h)[j]
17+
}
18+
19+
func (h *heap2558) Push(x any) {
20+
*h = append(*h, x.(int))
21+
}
22+
23+
func (h *heap2558) Pop() any {
24+
old := *h
25+
l := len(old)
26+
x := old[l-1]
27+
*h = old[:l-1]
428
return x
529
}
30+
31+
func Sqrt2558(x int) int {
32+
if x == 0 {
33+
return 0
34+
}
35+
36+
r := x
37+
for r*r > x {
38+
r = (r + x/r) / 2
39+
}
40+
41+
return r
42+
}
43+
44+
func Solution(gifts []int, k int) int64 {
45+
ans := int64(0)
46+
h := &heap2558{}
47+
for _, n := range gifts {
48+
heap.Push(h, n)
49+
}
50+
for ; k > 0; k-- {
51+
top := heap.Pop(h).(int)
52+
x := Sqrt2558(top)
53+
heap.Push(h, x)
54+
}
55+
for h.Len() > 0 {
56+
top := heap.Pop(h).(int)
57+
ans += int64(top)
58+
}
59+
return ans
60+
}

leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
gifts []int
14+
k int
15+
expect int64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{25, 64, 9, 4, 100}, 4, 29},
18+
{"TestCase2", []int{1, 1, 1, 1}, 4, 4},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.gifts, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.gifts, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)