Jesse and Cookies

  • + 0 comments

    C#:

    public static int cookies(int k, List<int> A)
        {
            var  items = new PriorityQueue<int, int>();
    
            for (int i = 0; i < A.Count; i++)
            {   
                items.Enqueue(A[i], A[i]);
            }
    
            int count = 0;
            while (items.Count > 1 && items.Peek() < k){
                var newItem = items.Dequeue() + (2 * items.Dequeue());
                
                items.Enqueue(newItem, newItem);
    
                count++;
            }
    
            return items.Peek() < k ? -1 : count;
        }