• + 0 comments

    C# Solution!

    public static int cookies(int k, List<int> A)
        {
            int count = 0;
            var pqueue = new PriorityQueue<int,int>();
        
            foreach(int e in A)
                pqueue.Enqueue(e,e);   
            
            while(pqueue.Count>1) {
                int x = pqueue.Dequeue();
                if(x>=k) {
                    return count;
                }
                else {
                    
                    int y = pqueue.Dequeue();
                    int c = x + (2*y);
                    pqueue.Enqueue(c,c);
                    count++;
                }
            }
            
            if(pqueue.Peek()>=k)
                return count;
            else
                return -1;    
        }