We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Jesse and Cookies
You are viewing a single comment's thread. Return to all comments →
Suboptimal TypeScript solution: class Queue {
}
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()}