forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
45 lines (39 loc) · 1001 Bytes
/
Solution.go
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
type AnimalShelf struct {
q [2][]int
}
func Constructor() AnimalShelf {
return AnimalShelf{}
}
func (this *AnimalShelf) Enqueue(animal []int) {
this.q[animal[1]] = append(this.q[animal[1]], animal[0])
}
func (this *AnimalShelf) DequeueAny() []int {
if len(this.q[0]) == 0 || (len(this.q[1]) > 0 && this.q[0][0] > this.q[1][0]) {
return this.DequeueDog()
}
return this.DequeueCat()
}
func (this *AnimalShelf) DequeueDog() []int {
if len(this.q[1]) == 0 {
return []int{-1, -1}
}
dog := this.q[1][0]
this.q[1] = this.q[1][1:]
return []int{dog, 1}
}
func (this *AnimalShelf) DequeueCat() []int {
if len(this.q[0]) == 0 {
return []int{-1, -1}
}
cat := this.q[0][0]
this.q[0] = this.q[0][1:]
return []int{cat, 0}
}
/**
* Your AnimalShelf object will be instantiated and called as such:
* obj := Constructor();
* obj.Enqueue(animal);
* param_2 := obj.DequeueAny();
* param_3 := obj.DequeueDog();
* param_4 := obj.DequeueCat();
*/