go: add «3264. Final Array State After K Multiplication Operations I»

URL:	https://leetcode.com/problems/final-array-state-after-k-multiplication-operations-i/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-12-16 14:16:02 +01:00
parent 1c25b8a606
commit 9a510e9fb9
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,35 @@
package main
import (
"cmp"
pq "github.com/emirpasic/gods/v2/queues/priorityqueue"
)
type Num struct {
value int
index int
}
func cmpNums(x, y Num) int {
return cmp.Or(cmp.Compare(x.value, y.value), cmp.Compare(x.index, y.index))
}
func getFinalState(nums []int, k int, multiplier int) []int {
numbers := pq.NewWith(cmpNums)
for i, num := range nums {
numbers.Enqueue(Num{value: num, index: i})
}
for range k {
candidate, ok := numbers.Dequeue()
if !ok {
panic("there should always be at least one candidate")
}
nums[candidate.index] *= multiplier
numbers.Enqueue(Num{value: candidate.value * multiplier, index: candidate.index})
}
return nums
}