Suck the cat with code! This article is participating in the cat essay Activity.

Adapted from: LeetCode- Interview question 03.06. Animal Shelter

The title

Use code to suck the cat at the cat shelter. One cat shelter has decided to follow a strict “first in, first out” policy to ensure every cat has a happy life. In order to better manage the cats and ensure their “physical integrity”, the shelter decided to separate cats according to gender when adopting them from the shelter. Adopters can only adopt the first cat to enter the shelter during the adoption process. In other words, adopters are not free to choose who they want to adopt. Create a data structure for this system that implements the various operation methods, enqueue, dequeueAny, dequeueMaleCat, and dequeueFemaleCat.

The enqueue method has an animal parameter, animal[0] represents the number of the cat, animal[1] represents the gender of the cat, where 0 represents the male cat and 1 represents the female cat.

The dequeue* method returns a list [cat number,, cat sex], or [-1,-1] if there are no adoptable animals.

Analysis of the

  • According to the title, the cat shelter complies"First in first out"Is suitable for useThe queueTo solve the problem.
  • Divided into two cohorts according to gender
  • throughpushFunction to add elements to the queue to achieve the action of receiving cats
  • throughshiftFunction to delete the first array element, implement adopt, meow action

steps

Define a CatShelf class and define queueMaleCat and queueFemaleCat to manage male and female cats respectively.

var CatShelf = function() {
    this.queueMaleCat = [];
    this.queueFemaleCat = [];
};
Copy the code

Through prototype chain, enqueue can receive cats, and they can join the queue respectively by judging the gender of cats

AnimalShelf.prototype.enqueue = function(animal) {
    if(animal[1] = =0) {this.queueMaleCat.push(animal[0]);
    } else {
        this.queueFemaleCat.push(animal[0]); }}Copy the code

Implement dequeueAny for cats through prototype chain. If there is no cat in the queue, go back to [-1,-1]. If there is no cat in the queue, go back to the first cat in the queue

AnimalShelf.prototype.dequeueAny = function() {
    if(this.queueMaleCat.length == 0 && this.queueFemaleCat.length == 0) {return [-1, -1];
    }
    if(this.queueMaleCat.length == 0) {return [this.queueMaleCat.shift(), 0];
    }
    if(this.queueFemaleCat.length == 0) {return [this.queueFemaleCat.shift(), 1];
    }
    return this.queueMaleCat[0] < this.queueFemaleCat[0]? [this.queueMaleCat.shift(), 0] : 
       [this.queueFemaleCat.shift(), 1];
};
Copy the code

DequeueMaleCat adopts male cats

AnimalShelf.prototype.dequeueMaleCat = function() {
    if(this.queueMaleCat.length == 0) return[-1, -1];
    return [this.queueMaleCat.shift(), 0];
};
Copy the code

Implement dequeueFemaleCat to adopt a female cat

AnimalShelf.prototype.dequeueFemaleCat = function() {
    if(this.queueFemaleCat.length == 0) return [-1, -1];
    return [this.queueFemaleCat.shift(), 1];
};
Copy the code

The complete code

var CatShelf = function() {
    this.queueMaleCat = [];
    this.queueFemaleCat = [];
};
/** 
 * @param {number[]} animal
 * @return {void}
 */
AnimalShelf.prototype.enqueue = function(animal) {
    if(animal[1] == 0){
        this.queueMaleCat.push(animal[0]);
    } else {
        this.queueFemaleCat.push(animal[0]);
    }
}
/**
 * @return {number[]}
 */
AnimalShelf.prototype.dequeueAny = function() {
    if(this.queueMaleCat.length == 0 && this.queueFemaleCat.length == 0){
        return [-1,-1];
    }
    if(this.queueMaleCat.length == 0){
        return [this.queueMaleCat.pop(), 1];
    }
    if(this.queueFemaleCat.length == 0){
        return [this.queueFemaleCat.shift(), 0];
    }
    return this.queueMaleCat[0] < this.queueFemaleCat[0] ? 
       [this.queueMaleCat.shift(), 0] : 
       [this.queueFemaleCat.shift(), 1];
};
/**
 * @return {number[]}
 */
AnimalShelf.prototype.dequeueMaleCat = function() {
    if(this.queueMaleCat.length == 0) return[-1, -1];
    return [this.queueMaleCat.shift(), 1];
};
/**
 * @return {number[]}
 */
AnimalShelf.prototype.dequeueFemaleCat = function() {
    if(this.queueFemaleCat.length == 0) return [-1, -1];
    return [this.queueFemaleCat.shift(), 0];
};
Copy the code

Finally, I hope that the majority of cat owners have decided to raise, please do not abandon. Shovelers are nicer to your cat!