Skip to content

Commit

Permalink
Create 1172.Dinner-Plate-Stacks.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Sep 30, 2019
1 parent 2bf8a67 commit 3adc22e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Design/1172.Dinner-Plate-Stacks/1172.Dinner-Plate-Stacks.cpp
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);
*/



0 comments on commit 3adc22e

Please sign in to comment.