You are viewing a single comment's thread. Return to all comments →
Simple golang solution with pq
type CookieHeap []int32 func (h CookieHeap) Len() int { return len(h) } func (h CookieHeap) Less(i, j int) bool { return h[i] < h[j] } func (h CookieHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *CookieHeap) Push(x interface{}) { *h = append(*h, x.(int32)) } func (h *CookieHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func cookies(k int32, A []int32) int32 { h := &CookieHeap{} heap.Init(h) for _, c := range A { heap.Push(h, c) } operations := int32(0) for len(*h) > 1 && (*h)[0] < k { least := heap.Pop(h).(int32) secondLeast := heap.Pop(h).(int32) sweetness := least + 2*secondLeast heap.Push(h, sweetness) operations++ } if len(*h) == 0 || (*h)[0] < k { return -1 } return operations }
Seems like cookies are disabled on this browser, please enable them to open this website
Jesse and Cookies
You are viewing a single comment's thread. Return to all comments →
Simple golang solution with pq