★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-vyimddna-kb.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
You have an infinite number of stacks arranged in a row and numbered (left to right) from 0, each of the stacks has the same maximum capacity
.git
Implement the DinnerPlates
class:github
DinnerPlates(int capacity)
Initializes the object with the maximum capacity
of the stacks.void push(int val)
pushes the given positive integer val
into the leftmost stack with size less than capacity
.int pop()
returns the value at the top of the rightmost non-empty stack and removes it from that stack, and returns -1
if all stacks are empty.int popAtStack(int index)
returns the value at the top of the stack with the given index
and removes it from that stack, and returns -1 if the stack with that given index
is empty.Example:微信
Input: ["DinnerPlates","push","push","push","push","push","popAtStack","push","push","popAtStack","popAtStack","pop","pop","pop","pop","pop"] [[2],[1],[2],[3],[4],[5],[0],[20],[21],[0],[2],[],[],[],[],[]] Output: [null,null,null,null,null,null,2,null,null,20,21,5,4,3,1,-1] Explanation: DinnerPlates D = DinnerPlates(2); // Initialize with capacity = 2 D.push(1); D.push(2); D.push(3); D.push(4); D.push(5); // The stacks are now: 2 4 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(0); // Returns 2. The stacks are now: 4 1 3 5 ﹈ ﹈ ﹈ D.push(20); // The stacks are now: 20 4 1 3 5 ﹈ ﹈ ﹈ D.push(21); // The stacks are now: 20 4 21 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(0); // Returns 20. The stacks are now: 4 21 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(2); // Returns 21. The stacks are now: 4 1 3 5 ﹈ ﹈ ﹈ D.pop() // Returns 5. The stacks are now: 4 1 3 ﹈ ﹈ D.pop() // Returns 4. The stacks are now: 1 3 ﹈ ﹈ D.pop() // Returns 3. The stacks are now: 1 ﹈ D.pop() // Returns 1. There are no stacks. D.pop() // Returns -1. There are still no stacks.
Constraints:app
1 <= capacity <= 20000
1 <= val <= 20000
0 <= index <= 100000
200000
calls will be made to push
, pop
, and popAtStack
.咱們把無限數量 ∞ 的棧排成一行,按從左到右的次序從 0 開始編號。每一個棧的的最大容量 capacity
都相同。less
實現一個叫「餐盤」的類 DinnerPlates
:spa
DinnerPlates(int capacity)
- 給出棧的最大容量 capacity
。void push(int val)
- 將給出的正整數 val
推入 從左往右第一個 沒有滿的棧。int pop()
- 返回 從右往左第一個 非空棧頂部的值,並將其從棧中刪除;若是全部的棧都是空的,請返回 -1
。int popAtStack(int index)
- 返回編號 index
的棧頂部的值,並將其從棧中刪除;若是編號 index
的棧是空的,請返回 -1
。示例:code
輸入: ["DinnerPlates","push","push","push","push","push","popAtStack","push","push","popAtStack","popAtStack","pop","pop","pop","pop","pop"] [[2],[1],[2],[3],[4],[5],[0],[20],[21],[0],[2],[],[],[],[],[]] 輸出: [null,null,null,null,null,null,2,null,null,20,21,5,4,3,1,-1] 解釋: DinnerPlates D = DinnerPlates(2); // 初始化,棧最大容量 capacity = 2 D.push(1); D.push(2); D.push(3); D.push(4); D.push(5); // 棧的現狀爲: 2 4 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(0); // 返回 2。棧的現狀爲: 4 1 3 5 ﹈ ﹈ ﹈ D.push(20); // 棧的現狀爲: 20 4 1 3 5 ﹈ ﹈ ﹈ D.push(21); // 棧的現狀爲: 20 4 21 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(0); // 返回 20。棧的現狀爲: 4 21 1 3 5 ﹈ ﹈ ﹈ D.popAtStack(2); // 返回 21。棧的現狀爲: 4 1 3 5 ﹈ ﹈ ﹈ D.pop() // 返回 5。棧的現狀爲: 4 1 3 ﹈ ﹈ D.pop() // 返回 4。棧的現狀爲: 1 3 ﹈ ﹈ D.pop() // 返回 3。棧的現狀爲: 1 ﹈ D.pop() // 返回 1。如今沒有棧。 D.pop() // 返回 -1。仍然沒有棧。
提示:htm
1 <= capacity <= 20000
1 <= val <= 20000
0 <= index <= 100000
push
,pop
,和 popAtStack
進行 200000
次調用。1 class DinnerPlates { 2 var map:[Int:[Int]] = [Int:[Int]]() 3 var cap:Int = 0 4 var curr:Int = 0 5 var last:Int = 0 6 var count:Int = 0 7 8 init(_ capacity: Int) { 9 self.cap = capacity 10 map[curr] = [Int]() 11 } 12 13 func push(_ val: Int) { 14 while(map[curr] != nil && map[curr,default:[Int]()].count == cap) 15 { 16 curr += 1 17 } 18 map[curr,default:[Int]()].append(val) 19 last = max(last, curr) 20 count += 1 21 22 } 23 24 func pop() -> Int { 25 if count == 0 {return -1} 26 while(last>=0 && map[last,default:[Int]()].isEmpty) 27 { 28 last -= 1 29 } 30 count -= 1 31 curr = min(curr, last) 32 return map[last,default:[Int]()].removeLast() 33 } 34 35 func popAtStack(_ index: Int) -> Int { 36 if(map[index] == nil || map[index,default:[Int]()].isEmpty) 37 { 38 return -1 39 } 40 count -= 1 41 curr = min(curr, index) 42 return map[index,default:[Int]()].removeLast() 43 } 44 } 45 46 /** 47 * Your DinnerPlates object will be instantiated and called as such: 48 * let obj = DinnerPlates(capacity) 49 * obj.push(val) 50 * let ret_2: Int = obj.pop() 51 * let ret_3: Int = obj.popAtStack(index) 52 */