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:
parent
1c25b8a606
commit
9a510e9fb9
1 changed files with 35 additions and 0 deletions
35
go/final-array-state-after-k-multiplication-operations-i.go
Normal file
35
go/final-array-state-after-k-multiplication-operations-i.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue