An animal shelter, which holds only dogs and cats, operates on a strictly"first in, first out" basis. People must adopt either the"oldest" (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 enqueue, dequeueAny, dequeueDog, and dequeueCat. You may use the built-in Linked list data structure.
enqueue method has a animal parameter, animal[0] represents the number of the animal, animal[1] represents the type of the animal, 0 for cat and 1 for dog.
dequeue* method returns [animal number, animal type], if there's no animal that can be adopted, return [-1, -1].
The number of animals in the shelter will not exceed 20000.
Solutions
Solution 1: Array of Queues
We define an array $q$ of length $2$ to store the queues of cats and dogs.
In the enqueue operation, assuming the animal number is $i$ and the animal type is $j$, we enqueue $i$ into $q[j]$.
In the dequeueAny operation, we check whether $q[0]$ is empty, or $q[1]$ is not empty and $q[1][0] < q[0][0]$. If so, we call dequeueDog, otherwise we call dequeueCat.
In the dequeueDog operation, if $q[1]$ is empty, we return $[-1, -1]$, otherwise we return $[q[1].pop(), 1]$.
In the dequeueCat operation, if $q[0]$ is empty, we return $[-1, -1]$, otherwise we return $[q[0].pop(), 0]$.
The time complexity of the above operations is $O(1)$, and the space complexity is $O(n)$, where $n$ is the number of animals in the animal shelter.
classAnimalShelf:def__init__(self):self.q=[deque(),deque()]defenqueue(self,animal:List[int])->None:i,j=animalself.q[j].append(i)defdequeueAny(self)->List[int]:ifnotself.q[0]or(self.q[1]andself.q[1][0]<self.q[0][0]):returnself.dequeueDog()returnself.dequeueCat()defdequeueDog(self)->List[int]:return[-1,-1]ifnotself.q[1]else[self.q[1].popleft(),1]defdequeueCat(self)->List[int]:return[-1,-1]ifnotself.q[0]else[self.q[0].popleft(),0]# Your AnimalShelf object will be instantiated and called as such:# obj = AnimalShelf()# obj.enqueue(animal)# param_2 = obj.dequeueAny()# param_3 = obj.dequeueDog()# param_4 = obj.dequeueCat()
classAnimalShelf{privateq:number[][]=[[],[]];constructor(){}enqueue(animal:number[]):void{const[i,j]=animal;this.q[j].push(i);}dequeueAny():number[]{if(this.q[0].length===0||(this.q[1].length>0&&this.q[0][0]>this.q[1][0])){returnthis.dequeueDog();}returnthis.dequeueCat();}dequeueDog():number[]{if(this.q[1].length===0){return[-1,-1];}return[this.q[1].shift()!,1];}dequeueCat():number[]{if(this.q[0].length===0){return[-1,-1];}return[this.q[0].shift()!,0];}}/** * Your AnimalShelf object will be instantiated and called as such: * var obj = new AnimalShelf() * obj.enqueue(animal) * var param_2 = obj.dequeueAny() * var param_3 = obj.dequeueDog() * var param_4 = obj.dequeueCat() */
classAnimalShelf{privatevarq:[[Int]]=Array(repeating:[],count:2)init(){}funcenqueue(_animal:[Int]){q[animal[1]].append(animal[0])}funcdequeueAny()->[Int]{ifq[0].isEmpty||(!q[1].isEmpty&&q[1].first!<q[0].first!){returndequeueDog()}returndequeueCat()}funcdequeueDog()->[Int]{returnq[1].isEmpty?[-1,-1]:[q[1].removeFirst(),1]}funcdequeueCat()->[Int]{returnq[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(); */