forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.swift
35 lines (28 loc) · 817 Bytes
/
Solution.swift
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
class AnimalShelf {
private var q: [[Int]] = Array(repeating: [], count: 2)
init() {
}
func enqueue(_ animal: [Int]) {
q[animal[1]].append(animal[0])
}
func dequeueAny() -> [Int] {
if q[0].isEmpty || (!q[1].isEmpty && q[1].first! < q[0].first!) {
return dequeueDog()
}
return dequeueCat()
}
func dequeueDog() -> [Int] {
return q[1].isEmpty ? [-1, -1] : [q[1].removeFirst(), 1]
}
func dequeueCat() -> [Int] {
return q[0].isEmpty ? [-1, -1] : [q[0].removeFirst(), 0]
}
}
/**
* Your AnimalShelf object will be instantiated and called as such:
* let obj = new AnimalShelf();
* obj.enqueue(animal);
* let param_2 = obj.dequeueAny();
* let param_3 = obj.dequeueDog();
* let param_4 = obj.dequeueCat();
*/