Skip to content

Commit bcc5be2

Browse files
committed
feat: add solutions to lcci problems: No.03.06
No.03.06.Animal Shelter
1 parent 04c4981 commit bcc5be2

File tree

4 files changed

+365
-0
lines changed

4 files changed

+365
-0
lines changed

lcci/03.06.Animal Shelter/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,131 @@ class AnimalShelf {
124124
*/
125125
```
126126

127+
### **TypeScript**
128+
129+
```ts
130+
class AnimalShelf {
131+
private cats: number[];
132+
private dogs: number[];
133+
134+
constructor() {
135+
this.cats = [];
136+
this.dogs = [];
137+
}
138+
139+
enqueue(animal: number[]): void {
140+
const [i, j] = animal;
141+
this[j === 0 ? 'cats' : 'dogs'].push(i);
142+
}
143+
144+
dequeueAny(): number[] {
145+
const n = this.dogs.length;
146+
const m = this.cats.length;
147+
if (n === 0 && m === 0) {
148+
return [-1, -1];
149+
}
150+
if ((this.dogs[0] ?? Infinity) < (this.cats[0] ?? Infinity)) {
151+
return [this.dogs.shift(), 1];
152+
} else {
153+
return [this.cats.shift(), 0];
154+
}
155+
}
156+
157+
dequeueDog(): number[] {
158+
if (this.dogs.length === 0) {
159+
return [-1, -1];
160+
}
161+
return [this.dogs.shift(), 1];
162+
}
163+
164+
dequeueCat(): number[] {
165+
if (this.cats.length === 0) {
166+
return [-1, -1];
167+
}
168+
return [this.cats.shift(), 0];
169+
}
170+
}
171+
172+
/**
173+
* Your AnimalShelf object will be instantiated and called as such:
174+
* var obj = new AnimalShelf()
175+
* obj.enqueue(animal)
176+
* var param_2 = obj.dequeueAny()
177+
* var param_3 = obj.dequeueDog()
178+
* var param_4 = obj.dequeueCat()
179+
*/
180+
```
181+
182+
### **Rust**
183+
184+
```rust
185+
use std::collections::VecDeque;
186+
187+
struct AnimalShelf {
188+
cats: VecDeque<i32>,
189+
dogs: VecDeque<i32>,
190+
}
191+
192+
/**
193+
* `&self` means the method takes an immutable reference.
194+
* If you need a mutable reference, change it to `&mut self` instead.
195+
*/
196+
impl AnimalShelf {
197+
fn new() -> Self {
198+
Self {
199+
cats: VecDeque::new(),
200+
dogs: VecDeque::new(),
201+
}
202+
}
203+
204+
fn enqueue(&mut self, animal: Vec<i32>) {
205+
if animal[1] == 0 {
206+
self.cats.push_back(animal[0]);
207+
} else {
208+
self.dogs.push_back(animal[0]);
209+
}
210+
}
211+
212+
fn dequeue_any(&mut self) -> Vec<i32> {
213+
match (self.cats.is_empty(), self.dogs.is_empty()) {
214+
(true, true) => vec![-1, -1],
215+
(true, false) => self.dequeue_dog(),
216+
(false, true) => self.dequeue_cat(),
217+
(false, false) => {
218+
if self.dogs[0] < self.cats[0] {
219+
self.dequeue_dog()
220+
} else {
221+
self.dequeue_cat()
222+
}
223+
}
224+
}
225+
}
226+
227+
fn dequeue_dog(&mut self) -> Vec<i32> {
228+
if self.dogs.is_empty() {
229+
return vec![-1, -1];
230+
}
231+
vec![self.dogs.pop_front().unwrap(), 1]
232+
}
233+
234+
fn dequeue_cat(&mut self) -> Vec<i32> {
235+
if self.cats.is_empty() {
236+
return vec![-1, -1];
237+
}
238+
vec![self.cats.pop_front().unwrap(), 0]
239+
}
240+
}
241+
242+
/**
243+
* Your AnimalShelf object will be instantiated and called as such:
244+
* let obj = AnimalShelf::new();
245+
* obj.enqueue(animal);
246+
* let ret_2: Vec<i32> = obj.dequeue_any();
247+
* let ret_3: Vec<i32> = obj.dequeue_dog();
248+
* let ret_4: Vec<i32> = obj.dequeue_cat();
249+
*/
250+
```
251+
127252
### **...**
128253

129254
```

lcci/03.06.Animal Shelter/README_EN.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,131 @@ class AnimalShelf {
129129
*/
130130
```
131131

132+
### **TypeScript**
133+
134+
```ts
135+
class AnimalShelf {
136+
private cats: number[];
137+
private dogs: number[];
138+
139+
constructor() {
140+
this.cats = [];
141+
this.dogs = [];
142+
}
143+
144+
enqueue(animal: number[]): void {
145+
const [i, j] = animal;
146+
this[j === 0 ? 'cats' : 'dogs'].push(i);
147+
}
148+
149+
dequeueAny(): number[] {
150+
const n = this.dogs.length;
151+
const m = this.cats.length;
152+
if (n === 0 && m === 0) {
153+
return [-1, -1];
154+
}
155+
if ((this.dogs[0] ?? Infinity) < (this.cats[0] ?? Infinity)) {
156+
return [this.dogs.shift(), 1];
157+
} else {
158+
return [this.cats.shift(), 0];
159+
}
160+
}
161+
162+
dequeueDog(): number[] {
163+
if (this.dogs.length === 0) {
164+
return [-1, -1];
165+
}
166+
return [this.dogs.shift(), 1];
167+
}
168+
169+
dequeueCat(): number[] {
170+
if (this.cats.length === 0) {
171+
return [-1, -1];
172+
}
173+
return [this.cats.shift(), 0];
174+
}
175+
}
176+
177+
/**
178+
* Your AnimalShelf object will be instantiated and called as such:
179+
* var obj = new AnimalShelf()
180+
* obj.enqueue(animal)
181+
* var param_2 = obj.dequeueAny()
182+
* var param_3 = obj.dequeueDog()
183+
* var param_4 = obj.dequeueCat()
184+
*/
185+
```
186+
187+
### **Rust**
188+
189+
```rust
190+
use std::collections::VecDeque;
191+
192+
struct AnimalShelf {
193+
cats: VecDeque<i32>,
194+
dogs: VecDeque<i32>,
195+
}
196+
197+
/**
198+
* `&self` means the method takes an immutable reference.
199+
* If you need a mutable reference, change it to `&mut self` instead.
200+
*/
201+
impl AnimalShelf {
202+
fn new() -> Self {
203+
Self {
204+
cats: VecDeque::new(),
205+
dogs: VecDeque::new(),
206+
}
207+
}
208+
209+
fn enqueue(&mut self, animal: Vec<i32>) {
210+
if animal[1] == 0 {
211+
self.cats.push_back(animal[0]);
212+
} else {
213+
self.dogs.push_back(animal[0]);
214+
}
215+
}
216+
217+
fn dequeue_any(&mut self) -> Vec<i32> {
218+
match (self.cats.is_empty(), self.dogs.is_empty()) {
219+
(true, true) => vec![-1, -1],
220+
(true, false) => self.dequeue_dog(),
221+
(false, true) => self.dequeue_cat(),
222+
(false, false) => {
223+
if self.dogs[0] < self.cats[0] {
224+
self.dequeue_dog()
225+
} else {
226+
self.dequeue_cat()
227+
}
228+
}
229+
}
230+
}
231+
232+
fn dequeue_dog(&mut self) -> Vec<i32> {
233+
if self.dogs.is_empty() {
234+
return vec![-1, -1];
235+
}
236+
vec![self.dogs.pop_front().unwrap(), 1]
237+
}
238+
239+
fn dequeue_cat(&mut self) -> Vec<i32> {
240+
if self.cats.is_empty() {
241+
return vec![-1, -1];
242+
}
243+
vec![self.cats.pop_front().unwrap(), 0]
244+
}
245+
}
246+
247+
/**
248+
* Your AnimalShelf object will be instantiated and called as such:
249+
* let obj = AnimalShelf::new();
250+
* obj.enqueue(animal);
251+
* let ret_2: Vec<i32> = obj.dequeue_any();
252+
* let ret_3: Vec<i32> = obj.dequeue_dog();
253+
* let ret_4: Vec<i32> = obj.dequeue_cat();
254+
*/
255+
```
256+
132257
### **...**
133258

134259
```

lcci/03.06.Animal Shelter/Solution.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::collections::VecDeque;
2+
3+
struct AnimalShelf {
4+
cats: VecDeque<i32>,
5+
dogs: VecDeque<i32>,
6+
}
7+
8+
/**
9+
* `&self` means the method takes an immutable reference.
10+
* If you need a mutable reference, change it to `&mut self` instead.
11+
*/
12+
impl AnimalShelf {
13+
fn new() -> Self {
14+
Self {
15+
cats: VecDeque::new(),
16+
dogs: VecDeque::new(),
17+
}
18+
}
19+
20+
fn enqueue(&mut self, animal: Vec<i32>) {
21+
if animal[1] == 0 {
22+
self.cats.push_back(animal[0]);
23+
} else {
24+
self.dogs.push_back(animal[0]);
25+
}
26+
}
27+
28+
fn dequeue_any(&mut self) -> Vec<i32> {
29+
match (self.cats.is_empty(), self.dogs.is_empty()) {
30+
(true, true) => vec![-1, -1],
31+
(true, false) => self.dequeue_dog(),
32+
(false, true) => self.dequeue_cat(),
33+
(false, false) => {
34+
if self.dogs[0] < self.cats[0] {
35+
self.dequeue_dog()
36+
} else {
37+
self.dequeue_cat()
38+
}
39+
}
40+
}
41+
}
42+
43+
fn dequeue_dog(&mut self) -> Vec<i32> {
44+
if self.dogs.is_empty() {
45+
return vec![-1, -1];
46+
}
47+
vec![self.dogs.pop_front().unwrap(), 1]
48+
}
49+
50+
fn dequeue_cat(&mut self) -> Vec<i32> {
51+
if self.cats.is_empty() {
52+
return vec![-1, -1];
53+
}
54+
vec![self.cats.pop_front().unwrap(), 0]
55+
}
56+
}
57+
58+
/**
59+
* Your AnimalShelf object will be instantiated and called as such:
60+
* let obj = AnimalShelf::new();
61+
* obj.enqueue(animal);
62+
* let ret_2: Vec<i32> = obj.dequeue_any();
63+
* let ret_3: Vec<i32> = obj.dequeue_dog();
64+
* let ret_4: Vec<i32> = obj.dequeue_cat();
65+
*/

lcci/03.06.Animal Shelter/Solution.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class AnimalShelf {
2+
private cats: number[];
3+
private dogs: number[];
4+
5+
constructor() {
6+
this.cats = [];
7+
this.dogs = [];
8+
}
9+
10+
enqueue(animal: number[]): void {
11+
const [i, j] = animal;
12+
this[j === 0 ? 'cats' : 'dogs'].push(i);
13+
}
14+
15+
dequeueAny(): number[] {
16+
const n = this.dogs.length;
17+
const m = this.cats.length;
18+
if (n === 0 && m === 0) {
19+
return [-1, -1];
20+
}
21+
if ((this.dogs[0] ?? Infinity) < (this.cats[0] ?? Infinity)) {
22+
return [this.dogs.shift(), 1];
23+
} else {
24+
return [this.cats.shift(), 0];
25+
}
26+
}
27+
28+
dequeueDog(): number[] {
29+
if (this.dogs.length === 0) {
30+
return [-1, -1];
31+
}
32+
return [this.dogs.shift(), 1];
33+
}
34+
35+
dequeueCat(): number[] {
36+
if (this.cats.length === 0) {
37+
return [-1, -1];
38+
}
39+
return [this.cats.shift(), 0];
40+
}
41+
}
42+
43+
/**
44+
* Your AnimalShelf object will be instantiated and called as such:
45+
* var obj = new AnimalShelf()
46+
* obj.enqueue(animal)
47+
* var param_2 = obj.dequeueAny()
48+
* var param_3 = obj.dequeueDog()
49+
* var param_4 = obj.dequeueCat()
50+
*/

0 commit comments

Comments
 (0)