Sort by

recency

|

362 Discussions

|

  • + 0 comments
    import heapq
    
    def cookies(k, A):
        step = 0
        heapq.heapify(A)
        
        while A:
            cookie = heapq.heappop(A)
            if cookie >= k:
                return step
            elif A:
                step += 1
                second_cookie = heapq.heappop(A)
                heapq.heappush(A, cookie + second_cookie * 2)
        return -1
    
  • + 0 comments

    PYTHON3 SOLUTION:

    import heapq
    class MinHeap:
        def __init__(self, A):
            self.harr = A[:]
            heapq.heapify(self.harr)
    
        def extractMin(self):
            return heapq.heappop(self.harr)
        
        def getMin(self):
            return self.harr[0]
        
        def getSize(self):
            return len(self.harr)
        
        def insertKey(self, k):
            heapq.heappush(self.harr, k)
    
    def cookies(k, A):
        # Write your code here
        h = MinHeap(A)
    
        res = 0
    
        while h.getMin() < k:
            if h.getSize() == 1:
                return -1
            
            first = h.extractMin()
            second = h.extractMin()
            h.insertKey(first + (second * 2))
    
            res += 1
        
        return res
    
  • + 1 comment
    # using heap sort
    import heapq
    def cookies(k, nums):
        # Write your code here
        heapq.heapify(nums)
        count =0
        
        while(nums):
            x = heapq.heappop(nums)
            if x>=k:
                return count
            
            if not nums:
                return -1
            y = heapq.heappop(nums)
            
            #formula
            n = x + (2*y)
            heapq.heappush(nums, n)
            count+=1
            
        # loop exits without giving count
        return -1
    
  • + 0 comments

    The example might have an error after [16,8,7,6], the next iteration should return a smaller array i.e. [20,16,8], but it still includes the 7 which should've been extracted.

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
        
            Scanner scan=new Scanner(System.in);
            int n=scan.nextInt();
            int k=scan.nextInt();
            PriorityQueue<Integer> queue=new PriorityQueue<Integer>();
            for(int i=0;i<n;i++)
                {
                queue.add(scan.nextInt());
            }
            int count=0;
            while(queue.peek()<k)
                {
                if(queue.size()>=2)
                    {
                int x=queue.remove();
                int y=queue.remove();
                y=x+2*y;
                queue.add(y);
                count++;
                }
                else
                    {
                    count=-1;
                    break;
                }
            }
            System.out.println(count);
        }
    }