生活随笔
收集整理的這篇文章主要介紹了
LeetCode 1172. 餐盘栈(栈 + set)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 題目
我們把無限數量 ∞ 的棧排成一行,按從左到右的次序從 0 開始編號。每個棧的的最大容量 capacity 都相同。
實現一個叫「餐盤」的類 DinnerPlates:
- DinnerPlates(int capacity) - 給出棧的最大容量 capacity。
- void push(int val) - 將給出的正整數 val 推入 從左往右第一個 沒有滿的棧。
- int pop() - 返回 從右往左第一個 非空棧頂部的值,并將其從棧中刪除;如果所有的棧都是空的,請返回 -1。
- int popAtStack(int index) - 返回編號 index 的棧頂部的值,并將其從棧中刪除;如果編號 index 的棧是空的,請返回 -1。
示例:
輸入:
["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);
D
.push(1);
D
.push(2);
D
.push(3);
D
.push(4);
D
.push(5); 1 3 5? ? ?
D
.popAtStack(0); 1 3 5? ? ?
D
.push(20); 1 3 5? ? ?
D
.push(21); 1 3 5? ? ?
D
.popAtStack(0); 1 3 5? ? ?
D
.popAtStack(2); 1 3 5? ? ?
D
.pop() 1 3 ? ?
D
.pop() ? ?
D
.pop() ?
D
.pop()
D
.pop() 提示:
1 <= capacity
<= 20000
1 <= val
<= 20000
0 <= index
<= 100000
最多會對 push,pop,和 popAtStack 進行
200000 次調用。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/dinner-plate-stacks
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
2. 解題
class STK
{
public:int size
;int capacity
;vector
<int> data
;STK(int cap
):size(0), capacity(cap
) { data
.resize(cap
);}bool isEmpty() const { return size
== 0;}bool isFull() const { return capacity
== size
;}void push(int val
){if(!isFull())data
[size
++] = val
;}int pop(){if(isEmpty())return -1;return data
[--size
];}
};
class DinnerPlates {int cap
;vector
<STK
> v
;
public:DinnerPlates(int capacity
) {cap
= capacity
;}void push(int val
) {int i
= 0;while(i
< v
.size() && v
[i
].isFull())i
++;if(i
< v
.size())v
[i
].push(val
);else{v
.push_back(STK(cap
));v
[i
].push(val
);}}int pop() {int i
= v
.size()-1;while(i
>= 0 && v
[i
].isEmpty())i
--;if(i
< 0)return -1;int tp
= v
[i
].pop();return tp
;}int popAtStack(int index
) {if(v
.empty() || index
>= v
.size())return -1;return v
[index
].pop();}
};
set
<int> s1
;
set
<int> s2
;
- s1 的 begin() 就是可以 push 的 id
- s2 的 end()-- 就是可以 pop 的 id,記得同時維護這兩個 set
class STK
{
public:int size
;int capacity
;vector
<int> data
;STK(int cap
):size(0), capacity(cap
) { data
.resize(cap
);}bool isEmpty() const { return size
== 0;}bool isFull() const { return capacity
== size
;}void push(int val
){if(!isFull())data
[size
++] = val
;}int pop(){if(isEmpty())return -1;return data
[--size
];}
};
class DinnerPlates {int cap
;vector
<STK
> v
;set
<int> s1
;set
<int> s2
;int tp
;
public:DinnerPlates(int capacity
) {cap
= capacity
;}void push(int val
) {if(!s1
.empty()){v
[*s1
.begin()].push(val
);s2
.insert(*s1
.begin());if(v
[*s1
.begin()].isFull())s1
.erase(s1
.begin());}else{v
.push_back(STK(cap
));v
[v
.size()-1].push(val
);s2
.insert(v
.size()-1);if(cap
!= 1)s1
.insert(v
.size()-1);}}int pop() {if(s2
.empty())return -1;tp
= v
[*(--s2
.end())].pop();s1
.insert(*(--s2
.end()));if(v
[*(--s2
.end())].isEmpty())s2
.erase(*(--s2
.end()));return tp
;}int popAtStack(int index
) {if(v
.empty() || index
>= v
.size())return -1;tp
= v
[index
].pop();if(v
[index
].isEmpty())s2
.erase(index
);s1
.insert(index
);return tp
;}
};
總結
以上是生活随笔為你收集整理的LeetCode 1172. 餐盘栈(栈 + set)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。