Minimum Queue with Constant Increment

There‘s a factory for cakes. Every day, the cost of producing a cake is different. The factory could produce as more as cakes in one day and the producing cost for cakes in the same day is the same. The shelf life for each cake is
s days and the storage fee for each cake is t dollars.

How to fulfill the customers‘ paying list with the minimum cost?

-----------------------------------------------------------------------------------

We could use a minimum queue to calculate the cost of day i+s as arr[i+s]. However, if we want to know arr[i+s+1], we have to refresh i+2 to i+s, i.e., we always calculates the minimum value of

a[i+1]   +   (s-1)t

a[i+2]   +   (s-2)t

a[i+3]   +   (s-3)t

....

a[i+s]   +    0t

Actually, there is no need to refresh i+2 to i+s. Another way is to refresh a[1] to end with adding a list of nt, (n-1)t.....0t in the beginning. Then minimum queue could used later.

时间: 2024-08-25 06:56:50

Minimum Queue with Constant Increment的相关文章

Linux I/O scheduler for solid-state drives

An I/O scheduler and a method for scheduling I/O requests to a solid-state drive (SSD) is disclosed. The I/O scheduler in accordance with the present disclosure bundles the write requests in such a form that the write requests in each bundle goes int

LeetCode 155 Min Stack(最小栈)

翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检索栈的最小元素 原文 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop

区别deque vector 和数组

Leetcode更新到155题了,这个easy的题acceptance却不高,我好奇的点开了它. Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get th

LeetCode(Easy)--C++笔记

前言:这是关于LeetCode上面练习题C++的笔记,有些地方参考有网友的解题方法(可能有些参考没能注明,望谅解),如有需要改进的地方希望留言指教,多谢! 目录: ZigZag Conversion Reverse digits of an integer Implement atoi to convert a string to an integer Determine whether an integer is a palindrome Write a function to find th

LeetCode Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

155. Min Stack 求栈的最小值

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

Min Stack leetcode

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

LeetCode OJ:Min Stack(最小栈问题)

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

Min Stack

1. Title 2. Http address https://leetcode.com/problems/min-stack/ 3. The question Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on