forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
39 lines (34 loc) · 936 Bytes
/
Solution.ts
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
class AnimalShelf {
private q: number[][] = [[], []];
constructor() {}
enqueue(animal: number[]): void {
const [i, j] = animal;
this.q[j].push(i);
}
dequeueAny(): number[] {
if (this.q[0].length === 0 || (this.q[1].length > 0 && this.q[0][0] > this.q[1][0])) {
return this.dequeueDog();
}
return this.dequeueCat();
}
dequeueDog(): number[] {
if (this.q[1].length === 0) {
return [-1, -1];
}
return [this.q[1].shift()!, 1];
}
dequeueCat(): number[] {
if (this.q[0].length === 0) {
return [-1, -1];
}
return [this.q[0].shift()!, 0];
}
}
/**
* Your AnimalShelf object will be instantiated and called as such:
* var obj = new AnimalShelf()
* obj.enqueue(animal)
* var param_2 = obj.dequeueAny()
* var param_3 = obj.dequeueDog()
* var param_4 = obj.dequeueCat()
*/