From ac642f72d7b696f9fea9761fc4a2dabbba8b1a9f Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 14 Oct 2024 17:22:20 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB2530.=20Maximal=20Score=20Aft?= =?UTF-8?q?er=20Applying=20K=20Operations=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/maximal-score-after-applying-k-operations/ Signed-off-by: Matej Focko --- ...ximal-score-after-applying-k-operations.go | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 go/maximal-score-after-applying-k-operations.go diff --git a/go/maximal-score-after-applying-k-operations.go b/go/maximal-score-after-applying-k-operations.go new file mode 100644 index 0000000..092dada --- /dev/null +++ b/go/maximal-score-after-applying-k-operations.go @@ -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 +}