go: add «3066. Minimum Operations to Exceed Threshold Value II»
URL: https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
3fd3eb1f4d
commit
84d3480c60
1 changed files with 25 additions and 0 deletions
25
go/minimum-operations-to-exceed-threshold-value-ii.go
Normal file
25
go/minimum-operations-to-exceed-threshold-value-ii.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
pq "github.com/emirpasic/gods/v2/queues/priorityqueue"
|
||||
)
|
||||
|
||||
func minOperations(nums []int, k int) int {
|
||||
t := int64(k)
|
||||
|
||||
q := pq.New[int64]()
|
||||
for _, x := range nums {
|
||||
q.Enqueue(int64(x))
|
||||
}
|
||||
|
||||
operations := 0
|
||||
for m, ok := q.Peek(); ok && m < t; m, ok = q.Peek() {
|
||||
x, _ := q.Dequeue()
|
||||
y, _ := q.Dequeue()
|
||||
|
||||
q.Enqueue(min(x, y)*2 + max(x, y))
|
||||
operations++
|
||||
}
|
||||
|
||||
return operations
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue