go: add «2530. Maximal Score After Applying K Operations»
URL: https://leetcode.com/problems/maximal-score-after-applying-k-operations/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
b50a4be64f
commit
ac642f72d7
1 changed files with 34 additions and 0 deletions
34
go/maximal-score-after-applying-k-operations.go
Normal file
34
go/maximal-score-after-applying-k-operations.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cmp"
|
||||||
|
|
||||||
|
pq "github.com/emirpasic/gods/v2/queues/priorityqueue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func maxKelements(nums []int, k int) int64 {
|
||||||
|
intCeil := func(whole, remainder int) int {
|
||||||
|
if remainder != 0 {
|
||||||
|
return whole + 1
|
||||||
|
}
|
||||||
|
return whole
|
||||||
|
}
|
||||||
|
|
||||||
|
descending := func(a, b int) int {
|
||||||
|
return -cmp.Compare(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
q := pq.NewWith(descending)
|
||||||
|
for _, x := range nums {
|
||||||
|
q.Enqueue(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
score := int64(0)
|
||||||
|
for i := 0; i < k; i++ {
|
||||||
|
num, _ := q.Dequeue()
|
||||||
|
score += int64(num)
|
||||||
|
|
||||||
|
q.Enqueue(intCeil(num/3, num%3))
|
||||||
|
}
|
||||||
|
return score
|
||||||
|
}
|
Loading…
Reference in a new issue