forked from Guo749/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2bf8a67
commit 3adc22e
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Design/1172.Dinner-Plate-Stacks/1172.Dinner-Plate-Stacks.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class DinnerPlates { | ||
unordered_map<int, vector<int>>Plate; | ||
int leftNotFull; | ||
int rightNotEmpty; | ||
int cap; | ||
|
||
public: | ||
DinnerPlates(int capacity) { | ||
leftNotFull = 0; | ||
rightNotEmpty = -1; | ||
cap = capacity; | ||
} | ||
|
||
void push(int val) { | ||
Plate[leftNotFull].push_back(val); | ||
while (Plate[leftNotFull].size()==cap) | ||
leftNotFull++; | ||
|
||
rightNotEmpty = max(rightNotEmpty, (Plate[leftNotFull].size()==0)?leftNotFull-1:leftNotFull); | ||
} | ||
|
||
int pop() { | ||
if (rightNotEmpty==-1) | ||
return -1; | ||
return popAtStack(rightNotEmpty); | ||
} | ||
|
||
int popAtStack(int index) { | ||
if (Plate[index].size()==0) | ||
return -1; | ||
|
||
int ret = Plate[index].back(); | ||
Plate[index].pop_back(); | ||
|
||
if (index==rightNotEmpty && Plate[rightNotEmpty].size()==0) | ||
{ | ||
while (Plate[rightNotEmpty].size()==0 && rightNotEmpty>=0) | ||
rightNotEmpty--; | ||
} | ||
|
||
leftNotFull = min(leftNotFull, index); | ||
|
||
return ret; | ||
} | ||
}; | ||
|
||
/** | ||
* Your DinnerPlates object will be instantiated and called as such: | ||
* DinnerPlates* obj = new DinnerPlates(capacity); | ||
* obj->push(val); | ||
* int param_2 = obj->pop(); | ||
* int param_3 = obj->popAtStack(index); | ||
*/ | ||
|
||
|
||
|