From 9a510e9fb99834ace1d3bd4972d64562a31bbed6 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 16 Dec 2024 14:16:02 +0100 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB3264.=20Final=20Array=20State?= =?UTF-8?q?=20After=20K=20Multiplication=20Operations=20I=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/final-array-state-after-k-multiplication-operations-i/ Signed-off-by: Matej Focko --- ...ate-after-k-multiplication-operations-i.go | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 go/final-array-state-after-k-multiplication-operations-i.go diff --git a/go/final-array-state-after-k-multiplication-operations-i.go b/go/final-array-state-after-k-multiplication-operations-i.go new file mode 100644 index 0000000..206e3ae --- /dev/null +++ b/go/final-array-state-after-k-multiplication-operations-i.go @@ -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 +}