diff --git a/lcci/03.06.Animal Shelter/README.md b/lcci/03.06.Animal Shelter/README.md index e1574c778a09c..6dfe404e984d4 100644 --- a/lcci/03.06.Animal Shelter/README.md +++ b/lcci/03.06.Animal Shelter/README.md @@ -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(); + */ +``` + diff --git a/lcci/03.06.Animal Shelter/README_EN.md b/lcci/03.06.Animal Shelter/README_EN.md index 0b53a6cff2100..77ffa863d6080 100644 --- a/lcci/03.06.Animal Shelter/README_EN.md +++ b/lcci/03.06.Animal Shelter/README_EN.md @@ -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(); + */ +``` + diff --git a/lcci/03.06.Animal Shelter/Solution.swift b/lcci/03.06.Animal Shelter/Solution.swift new file mode 100644 index 0000000000000..7a78f0c570e8f --- /dev/null +++ b/lcci/03.06.Animal Shelter/Solution.swift @@ -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(); + */ + \ No newline at end of file