• + 0 comments

    Hi everyone, i need help :), well i'm alwayst get time limit at case 20 - 24. I already try to optimize my code using heap method. But i still got time limit, This is my function in go lang: func cookies(k int, pq *IntHeap) int32 {

    var count int32
    // for i := 0 ; i < len(A) ; i++{
    //     heap.Push(pq,A[i])
    // }
    for pq.Len() > 0{
        smallA := heap.Pop(pq).(int)
    
        if smallA < k {
            if pq.Len() == 0{
                count = -1
                break
            }else{
                smallB := heap.Pop(pq).(int)
                sum := smallA + 2 * smallB
                heap.Push(pq,sum)
                count++
                fmt.Println(smallA,smallB,sum)
            }
        }else{
            break
        }
    }
    

    I hope somebody can give me tips for this problem :) Thanks everyone

    return count
    

    }