forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
46 lines (40 loc) · 1.02 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class AnimalShelf {
public:
AnimalShelf() {
}
void enqueue(vector<int> animal) {
q[animal[1]].push(animal[0]);
}
vector<int> dequeueAny() {
if (q[0].empty() || (!q[1].empty() && q[1].front() < q[0].front())) {
return dequeueDog();
}
return dequeueCat();
}
vector<int> dequeueDog() {
if (q[1].empty()) {
return {-1, -1};
}
int dog = q[1].front();
q[1].pop();
return {dog, 1};
}
vector<int> dequeueCat() {
if (q[0].empty()) {
return {-1, -1};
}
int cat = q[0].front();
q[0].pop();
return {cat, 0};
}
private:
queue<int> q[2];
};
/**
* Your AnimalShelf object will be instantiated and called as such:
* AnimalShelf* obj = new AnimalShelf();
* obj->enqueue(animal);
* vector<int> param_2 = obj->dequeueAny();
* vector<int> param_3 = obj->dequeueDog();
* vector<int> param_4 = obj->dequeueCat();
*/