Jesse and Cookies

  • + 0 comments

    Python, using stack for premade cookies, queue for new cookies

    def getsmallest(stack, queue):
        if len(stack) == 0:
            return queue.popleft()
        elif len(queue) == 0:
            return stack.pop()
        return queue.popleft() if queue[0] < stack[-1] else stack.pop()
        
    def cookies(k, startstack):
        startstack.sort(reverse=True)
        createdqueue = deque()
        numops = 0
        while True:
            if (len(startstack) == 0 or startstack[-1] >= k) and (len(createdqueue) == 0 or createdqueue[0] >= k):
                return numops
            if (len(startstack) + len(createdqueue) < 2):
                return -1
                
            numops += 1
            s1 = getsmallest(startstack, createdqueue)
            s2 = getsmallest(startstack, createdqueue)
            createdqueue.append(s1 + 2 * s2)