Jesse and Cookies

  • + 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
    

    }