go: add «2593. Find Score of an Array After Marking All Elements»

URL:	https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-12-14 00:05:35 +01:00
parent da959699dd
commit c15de5ee79
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,39 @@
package main
import (
"cmp"
pq "github.com/emirpasic/gods/v2/queues/priorityqueue"
)
type Candidate struct {
value int
index int
}
func smallestFirst(l, r Candidate) int {
return cmp.Or(cmp.Compare(l.value, r.value), cmp.Compare(l.index, r.index))
}
func findScore(nums []int) int64 {
candidates := pq.NewWith(smallestFirst)
for i, num := range nums {
candidates.Enqueue(Candidate{value: num, index: i})
}
score, marked := int64(0), make([]bool, len(nums))
for nextCandidate, ok := candidates.Dequeue(); ok; nextCandidate, ok = candidates.Dequeue() {
if marked[nextCandidate.index] {
continue
}
score += int64(nextCandidate.value)
for _, index := range []int{nextCandidate.index - 1, nextCandidate.index, nextCandidate.index + 1} {
if index >= 0 && index < len(marked) {
marked[index] = true
}
}
}
return score
}