Skip to content

Commit 0e0d120

Browse files
committed
feat: add solutions to lcci question: 03.06.Animal Shelter
添加《程序员面试金典》题解:面试题 03.06. 动物收容所
1 parent 0ec34e5 commit 0e0d120

File tree

4 files changed

+288
-58
lines changed

4 files changed

+288
-58
lines changed

lcci/03.06.Animal Shelter/README.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,85 @@
3535

3636
## 解法
3737
<!-- 这里可写通用的实现逻辑 -->
38-
38+
双队列存储。
3939

4040
### Python3
4141
<!-- 这里可写当前语言的特殊实现逻辑 -->
4242

4343
```python
44+
class AnimalShelf:
45+
46+
def __init__(self):
47+
self.cats = []
48+
self.dogs = []
49+
50+
def enqueue(self, animal: List[int]) -> None:
51+
if animal[1] == 0:
52+
self.cats.insert(0, animal[0])
53+
else:
54+
self.dogs.insert(0, animal[0])
55+
56+
def dequeueAny(self) -> List[int]:
57+
if len(self.dogs) == 0: return self.dequeueCat()
58+
if len(self.cats) == 0: return self.dequeueDog()
59+
return self.dequeueDog() if self.dogs[-1] < self.cats[-1] else self.dequeueCat()
4460

61+
def dequeueDog(self) -> List[int]:
62+
return [-1, -1] if len(self.dogs) == 0 else [self.dogs.pop(), 1]
63+
64+
def dequeueCat(self) -> List[int]:
65+
return [-1, -1] if len(self.cats) == 0 else [self.cats.pop(), 0]
66+
67+
68+
# Your AnimalShelf object will be instantiated and called as such:
69+
# obj = AnimalShelf()
70+
# obj.enqueue(animal)
71+
# param_2 = obj.dequeueAny()
72+
# param_3 = obj.dequeueDog()
73+
# param_4 = obj.dequeueCat()
4574
```
4675

4776
### Java
4877
<!-- 这里可写当前语言的特殊实现逻辑 -->
4978

5079
```java
51-
80+
class AnimalShelf {
81+
Queue<Integer> cats;
82+
Queue<Integer> dogs;
83+
public AnimalShelf() {
84+
cats = new LinkedList<>();
85+
dogs = new LinkedList<>();
86+
}
87+
88+
public void enqueue(int[] animal) {
89+
if (animal[1] == 0) {
90+
cats.offer(animal[0]);
91+
} else {
92+
dogs.offer(animal[0]);
93+
}
94+
}
95+
96+
public int[] dequeueAny() {
97+
return dogs.isEmpty() ? dequeueCat() : (cats.isEmpty() ? dequeueDog() : (dogs.peek() < cats.peek() ? dequeueDog() : dequeueCat()));
98+
}
99+
100+
public int[] dequeueDog() {
101+
return dogs.isEmpty() ? new int[]{-1, -1} : new int[]{dogs.poll(), 1};
102+
}
103+
104+
public int[] dequeueCat() {
105+
return cats.isEmpty() ? new int[]{-1, -1} : new int[]{cats.poll(), 0};
106+
}
107+
}
108+
109+
/**
110+
* Your AnimalShelf object will be instantiated and called as such:
111+
* AnimalShelf obj = new AnimalShelf();
112+
* obj.enqueue(animal);
113+
* int[] param_2 = obj.dequeueAny();
114+
* int[] param_3 = obj.dequeueDog();
115+
* int[] param_4 = obj.dequeueCat();
116+
*/
52117
```
53118

54119
### ...
Lines changed: 152 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,152 @@
1-
# [03.06. Animal Shelter](https://leetcode-cn.com/problems/animal-shelter-lcci)
2-
3-
## Description
4-
<p>An animal shelter, which holds only dogs and cats, operates on a strictly&quot;first in, first out&quot; basis. People must adopt either the&quot;oldest&quot; (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data structures to maintain this system and implement operations such as <code>enqueue</code>, <code>dequeueAny</code>, <code>dequeueDog</code>, and <code>dequeueCat</code>. You may use the built-in Linked list data structure.</p>
5-
6-
<p><code>enqueue</code> method has a <code>animal</code> parameter, <code>animal[0]</code> represents the number of the animal, <code>animal[1]</code> represents the type of the animal, 0 for cat and 1 for dog.</p>
7-
8-
<p><code>dequeue*</code> method returns <code>[animal number, animal type]</code>, if there&#39;s no animal that can be adopted, return <code>[-1, -1]</code>.</p>
9-
10-
<p><strong>Example1:</strong></p>
11-
12-
<pre>
13-
<strong> Input</strong>:
14-
[&quot;AnimalShelf&quot;, &quot;enqueue&quot;, &quot;enqueue&quot;, &quot;dequeueCat&quot;, &quot;dequeueDog&quot;, &quot;dequeueAny&quot;]
15-
[[], [[0, 0]], [[1, 0]], [], [], []]
16-
<strong> Output</strong>:
17-
[null,null,null,[0,0],[-1,-1],[1,0]]
18-
</pre>
19-
20-
<p><strong>Example2:</strong></p>
21-
22-
<pre>
23-
<strong> Input</strong>:
24-
[&quot;AnimalShelf&quot;, &quot;enqueue&quot;, &quot;enqueue&quot;, &quot;enqueue&quot;, &quot;dequeueDog&quot;, &quot;dequeueCat&quot;, &quot;dequeueAny&quot;]
25-
[[], [[0, 0]], [[1, 0]], [[2, 1]], [], [], []]
26-
<strong> Output</strong>:
27-
[null,null,null,null,[2,1],[0,0],[1,0]]
28-
</pre>
29-
30-
<p><strong>Note:</strong></p>
31-
32-
<ol>
33-
<li>The number of animals in the shelter will not exceed 20000.</li>
34-
</ol>
35-
36-
37-
38-
## Solutions
39-
40-
41-
### Python3
42-
43-
```python
44-
45-
```
46-
47-
### Java
48-
49-
```java
50-
51-
```
52-
53-
### ...
54-
```
55-
56-
```
1+
# [03.06. Animal Shelter](https://leetcode-cn.com/problems/animal-shelter-lcci)
2+
3+
## Description
4+
<p>An animal shelter, which holds only dogs and cats, operates on a strictly&quot;first in, first out&quot; basis. People must adopt either the&quot;oldest&quot; (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data structures to maintain this system and implement operations such as <code>enqueue</code>, <code>dequeueAny</code>, <code>dequeueDog</code>, and <code>dequeueCat</code>. You may use the built-in Linked list data structure.</p>
5+
6+
7+
8+
<p><code>enqueue</code> method has a <code>animal</code> parameter, <code>animal[0]</code> represents the number of the animal, <code>animal[1]</code> represents the type of the animal, 0 for cat and 1 for dog.</p>
9+
10+
11+
12+
<p><code>dequeue*</code> method returns <code>[animal number, animal type]</code>, if there&#39;s no animal that can be adopted, return <code>[-1, -1]</code>.</p>
13+
14+
15+
16+
<p><strong>Example1:</strong></p>
17+
18+
19+
20+
<pre>
21+
22+
<strong> Input</strong>:
23+
24+
[&quot;AnimalShelf&quot;, &quot;enqueue&quot;, &quot;enqueue&quot;, &quot;dequeueCat&quot;, &quot;dequeueDog&quot;, &quot;dequeueAny&quot;]
25+
26+
[[], [[0, 0]], [[1, 0]], [], [], []]
27+
28+
<strong> Output</strong>:
29+
30+
[null,null,null,[0,0],[-1,-1],[1,0]]
31+
32+
</pre>
33+
34+
35+
36+
<p><strong>Example2:</strong></p>
37+
38+
39+
40+
<pre>
41+
42+
<strong> Input</strong>:
43+
44+
[&quot;AnimalShelf&quot;, &quot;enqueue&quot;, &quot;enqueue&quot;, &quot;enqueue&quot;, &quot;dequeueDog&quot;, &quot;dequeueCat&quot;, &quot;dequeueAny&quot;]
45+
46+
[[], [[0, 0]], [[1, 0]], [[2, 1]], [], [], []]
47+
48+
<strong> Output</strong>:
49+
50+
[null,null,null,null,[2,1],[0,0],[1,0]]
51+
52+
</pre>
53+
54+
55+
56+
<p><strong>Note:</strong></p>
57+
58+
59+
60+
<ol>
61+
62+
<li>The number of animals in the shelter will not exceed 20000.</li>
63+
64+
</ol>
65+
66+
67+
68+
69+
## Solutions
70+
71+
72+
### Python3
73+
74+
```python
75+
class AnimalShelf:
76+
77+
def __init__(self):
78+
self.cats = []
79+
self.dogs = []
80+
81+
def enqueue(self, animal: List[int]) -> None:
82+
if animal[1] == 0:
83+
self.cats.insert(0, animal[0])
84+
else:
85+
self.dogs.insert(0, animal[0])
86+
87+
def dequeueAny(self) -> List[int]:
88+
if len(self.dogs) == 0: return self.dequeueCat()
89+
if len(self.cats) == 0: return self.dequeueDog()
90+
return self.dequeueDog() if self.dogs[-1] < self.cats[-1] else self.dequeueCat()
91+
92+
def dequeueDog(self) -> List[int]:
93+
return [-1, -1] if len(self.dogs) == 0 else [self.dogs.pop(), 1]
94+
95+
def dequeueCat(self) -> List[int]:
96+
return [-1, -1] if len(self.cats) == 0 else [self.cats.pop(), 0]
97+
98+
99+
# Your AnimalShelf object will be instantiated and called as such:
100+
# obj = AnimalShelf()
101+
# obj.enqueue(animal)
102+
# param_2 = obj.dequeueAny()
103+
# param_3 = obj.dequeueDog()
104+
# param_4 = obj.dequeueCat()
105+
```
106+
107+
### Java
108+
109+
```java
110+
class AnimalShelf {
111+
Queue<Integer> cats;
112+
Queue<Integer> dogs;
113+
public AnimalShelf() {
114+
cats = new LinkedList<>();
115+
dogs = new LinkedList<>();
116+
}
117+
118+
public void enqueue(int[] animal) {
119+
if (animal[1] == 0) {
120+
cats.offer(animal[0]);
121+
} else {
122+
dogs.offer(animal[0]);
123+
}
124+
}
125+
126+
public int[] dequeueAny() {
127+
return dogs.isEmpty() ? dequeueCat() : (cats.isEmpty() ? dequeueDog() : (dogs.peek() < cats.peek() ? dequeueDog() : dequeueCat()));
128+
}
129+
130+
public int[] dequeueDog() {
131+
return dogs.isEmpty() ? new int[]{-1, -1} : new int[]{dogs.poll(), 1};
132+
}
133+
134+
public int[] dequeueCat() {
135+
return cats.isEmpty() ? new int[]{-1, -1} : new int[]{cats.poll(), 0};
136+
}
137+
}
138+
139+
/**
140+
* Your AnimalShelf object will be instantiated and called as such:
141+
* AnimalShelf obj = new AnimalShelf();
142+
* obj.enqueue(animal);
143+
* int[] param_2 = obj.dequeueAny();
144+
* int[] param_3 = obj.dequeueDog();
145+
* int[] param_4 = obj.dequeueCat();
146+
*/
147+
```
148+
149+
### ...
150+
```
151+
152+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class AnimalShelf {
2+
Queue<Integer> cats;
3+
Queue<Integer> dogs;
4+
public AnimalShelf() {
5+
cats = new LinkedList<>();
6+
dogs = new LinkedList<>();
7+
}
8+
9+
public void enqueue(int[] animal) {
10+
if (animal[1] == 0) {
11+
cats.offer(animal[0]);
12+
} else {
13+
dogs.offer(animal[0]);
14+
}
15+
}
16+
17+
public int[] dequeueAny() {
18+
return dogs.isEmpty() ? dequeueCat() : (cats.isEmpty() ? dequeueDog() : (dogs.peek() < cats.peek() ? dequeueDog() : dequeueCat()));
19+
}
20+
21+
public int[] dequeueDog() {
22+
return dogs.isEmpty() ? new int[]{-1, -1} : new int[]{dogs.poll(), 1};
23+
}
24+
25+
public int[] dequeueCat() {
26+
return cats.isEmpty() ? new int[]{-1, -1} : new int[]{cats.poll(), 0};
27+
}
28+
}
29+
30+
/**
31+
* Your AnimalShelf object will be instantiated and called as such:
32+
* AnimalShelf obj = new AnimalShelf();
33+
* obj.enqueue(animal);
34+
* int[] param_2 = obj.dequeueAny();
35+
* int[] param_3 = obj.dequeueDog();
36+
* int[] param_4 = obj.dequeueCat();
37+
*/

lcci/03.06.Animal Shelter/Solution.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class AnimalShelf:
2+
3+
def __init__(self):
4+
self.cats = []
5+
self.dogs = []
6+
7+
def enqueue(self, animal: List[int]) -> None:
8+
if animal[1] == 0:
9+
self.cats.insert(0, animal[0])
10+
else:
11+
self.dogs.insert(0, animal[0])
12+
13+
def dequeueAny(self) -> List[int]:
14+
if len(self.dogs) == 0:
15+
return self.dequeueCat()
16+
if len(self.cats) == 0:
17+
return self.dequeueDog()
18+
return self.dequeueDog() if self.dogs[-1] < self.cats[-1] else self.dequeueCat()
19+
20+
def dequeueDog(self) -> List[int]:
21+
return [-1, -1] if len(self.dogs) == 0 else [self.dogs.pop(), 1]
22+
23+
def dequeueCat(self) -> List[int]:
24+
return [-1, -1] if len(self.cats) == 0 else [self.cats.pop(), 0]
25+
26+
27+
# Your AnimalShelf object will be instantiated and called as such:
28+
# obj = AnimalShelf()
29+
# obj.enqueue(animal)
30+
# param_2 = obj.dequeueAny()
31+
# param_3 = obj.dequeueDog()
32+
# param_4 = obj.dequeueCat()

0 commit comments

Comments
 (0)