Jesse and Cookies

Sort by

recency

|

36 Discussions

|

  • + 0 comments
    from heapq import heappush, heappop, heapify
    
    def cookies(k, A):
        heapify(A)
        i = 0
        while A[0] < k and len(A) > 1:
            heappush(A, heappop(A) + (heappop(A) * 2))
            i += 1
        return i if A[0] >= k else -1
    
  • + 0 comments

    Suboptimal TypeScript solution: class Queue {

    private enqueueStack: number[];
    private dequeueStack: number[];
    
    constructor() {
        this.enqueueStack = []
        this.dequeueStack = []
    }
    
    enqueue(value:number) {
        this.enqueueStack.push(value)
    }
    
    dequeue(): number | undefined {
        if (this.dequeueStack.length === 0) {
            while(this.enqueueStack.length > 0) {
                let value:number = this.enqueueStack.pop()
                this.dequeueStack.push(value)
            }
        }
    
        return this.dequeueStack.pop()
    }
    
    // Peek at the front element of the queue
    front(): number | undefined {
        if (this.dequeueStack.length > 0) {
            return this.dequeueStack[this.dequeueStack.length - 1];
        } else if (this.enqueueStack.length > 0) {
            return this.enqueueStack[0];
        }
        return undefined;
    }
    
    // Check if the queue is empty
    isEmpty(): boolean {
        return this.enqueueStack.length === 0 && this.dequeueStack.length === 0;
    }
    

    }

    function cookies(k: number, A: number[]): number { // Write your code here A.sort((a,b) => b - a) //console.log(sorted: ${A}) let superCookies:Queue = new Queue()

    let smallest:number = A.pop()
    let secondSmallest:number = A.pop()
    let iterations:number = 0
    
    //console.log(`smallest: `${smallest}, secondSmallest: $`{secondSmallest}`)
    
    while(smallest < k && !(smallest === undefined || secondSmallest == undefined)) {
        iterations++
        let superCookie:number = smallest + (2 * secondSmallest)
        //console.log(`Added super cookie: ${superCookie}`)
        superCookies.enqueue(superCookie)
    
        let smallestSuperCookie:number = superCookies.front()
        if (smallestSuperCookie === undefined) smallestSuperCookie = 1000000000
    
        let smallestArrValue:number | undefined = A[A.length - 1]
        if (smallestArrValue === undefined) smallestArrValue = 1000000000
    
        //console.log(`Smallest super cookie: `${smallestSuperCookie}, smallestArrValue: $`{smallestArrValue}`)
    
    
        if (smallestSuperCookie < smallestArrValue) {
            smallest = superCookies.dequeue()
        } else {
            smallest = A.pop()
        }
    
        //console.log(`Smallest: ${smallest}`)
    
    
        smallestSuperCookie = superCookies.front()
        if (smallestSuperCookie === undefined) smallestSuperCookie = 1000000000
    
        smallestArrValue = A[A.length - 1]
        if (smallestArrValue === undefined) smallestArrValue = 1000000000
    
        //console.log(`Smallest super cookie: `${smallestSuperCookie}, smallestArrValue: $`{smallestArrValue}`)
    
    
        if (smallestSuperCookie < smallestArrValue) {
            secondSmallest = superCookies.dequeue()
        } else {
            secondSmallest = A.pop()
        }
    
        //console.log(`secondSmallest: ${secondSmallest}`)
    
    }
    
    if (smallest < k) {
        return -1
    }
    
    return iterations
    

    }

  • + 0 comments

    here is hackerrank jesse and cookies problem solution in Python, java, c++, c and javascript

  • + 0 comments

    In JAVA8 :

    public static int cookies(int k, List<Integer> A) {
            // Write your code here
            PriorityQueue <Integer> pq = new PriorityQueue<>(A);        
            int count = 0;
            while (pq.size() > 1 && pq.peek()<k) {
                pq.offer(pq.poll() + 2 * pq.poll());
                count++;
            }
            return  pq.peek() >= k ? count : -1;
        }
    
  • + 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;
        }