-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue03_06.java
More file actions
68 lines (61 loc) · 1.67 KB
/
Queue03_06.java
File metadata and controls
68 lines (61 loc) · 1.67 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package queue;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
/**
* @ProjectName: leetcode
* @Package: queue
* @ClassName: Queue03_06
* @Author: markey
* @Description:
* @Date: 2020/6/4 21:48
* @Version: 1.0
*/
public class Queue03_06 {
class AnimalShelf {
Queue<Integer> cats;
Queue<Integer> dogs;
public AnimalShelf() {
cats = new LinkedList<>();
dogs = new LinkedList<>();
}
public void enqueue(int[] animal) {
if (animal[1] == 0) {
cats.offer(animal[0]);
} else {
dogs.offer(animal[0]);
}
}
public int[] dequeueAny() {
int[] res = new int[] {-1, -1};
if (dogs.size() <= 0 && cats.size() <= 0) {
return res;
}
if (dogs.size() == 0) {
return new int[] {cats.poll(), 0};
} else if (cats.size() == 0){
return new int[] {dogs.poll(), 1};
} else {
if (cats.peek() > dogs.peek()) {
return new int[] {dogs.poll(), 1};
} else {
return new int[] {cats.poll(), 0};
}
}
}
public int[] dequeueDog() {
if (dogs.size() > 0) {
return new int[]{dogs.poll(), 1};
} else {
return new int[] {-1, -1};
}
}
public int[] dequeueCat() {
if (cats.size() > 0) {
return new int[]{cats.poll(), 0};
} else {
return new int[] {-1, -1};
}
}
}
}