Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lcci/03.06.Animal Shelter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,43 @@ impl AnimalShelf {
*/
```

```swift
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();
*/
```

<!-- tabs:end -->

<!-- end -->
37 changes: 37 additions & 0 deletions lcci/03.06.Animal Shelter/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,43 @@ impl AnimalShelf {
*/
```

```swift
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();
*/
```

<!-- tabs:end -->

<!-- end -->
35 changes: 35 additions & 0 deletions lcci/03.06.Animal Shelter/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
*/