-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1061 from 0xff-dev/2558
Add solution and test-cases for problem 2558
- Loading branch information
Showing
3 changed files
with
108 additions
and
11 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
57 changes: 56 additions & 1 deletion
57
leetcode/2501-2600/2558.Take-Gifts-From-the-Richest-Pile/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters