leetcode 面试题 03.06. 动物收容所

发布于 2022-09-26
面试题 03.06. 动物收容所

面试题 03.06. 动物收容所

题目

动物收容所。有家动物收容所只收容狗与猫,且严格遵守“先进先出”的原则。在收养该收容所的动物时,收养人只能收养所有动物中“最老”(由其进入收容所的时间长短而定)的动物,或者可以挑选猫或狗(同时必须收养此类动物中“最老”的)。换言之,收养人不能自由挑选想收养的对象。请创建适用于这个系统的数据结构,实现各种操作方法,比如enqueuedequeueAnydequeueDogdequeueCat。允许使用Java内置的LinkedList数据结构。

enqueue方法有一个animal参数,animal[0]代表动物编号,animal[1]代表动物种类,其中 0 代表猫,1 代表狗。

dequeue*方法返回一个列表[动物编号, 动物种类],若没有可以收养的动物,则返回[-1,-1]

示例1:

 输入:
["AnimalShelf", "enqueue", "enqueue", "dequeueCat", "dequeueDog", "dequeueAny"]
[[], [[0, 0]], [[1, 0]], [], [], []]
 输出:
[null,null,null,[0,0],[-1,-1],[1,0]]

示例2:

 输入:
["AnimalShelf", "enqueue", "enqueue", "enqueue", "dequeueDog", "dequeueCat", "dequeueAny"]
[[], [[0, 0]], [[1, 0]], [[2, 1]], [], [], []]
 输出:
[null,null,null,null,[2,1],[0,0],[1,0]]

说明:

  1. 收纳所的最大容量为20000

题解

采用双队列实现,猫和狗分开存储,取任意一个时,则判断哪个的序号最小进行返回。

import java.util.LinkedList;

class AnimalShelf {

    private LinkedList<int[]> dogList;
    
    private LinkedList<int[]> catList;
    
    private int[] emptyAnimal = new int[]{-1, -1};
    
    public AnimalShelf() {
        dogList = new LinkedList<>();
        catList = new LinkedList<>();
    }
    
    public void enqueue(int[] animal) {
        switch (animal[1]) {
            case 0: catList.addLast(animal); break;
            case 1: dogList.addLast(animal); break;
        }
    }
    
    public int[] dequeueAny() {
        if (dogList.isEmpty() && catList.isEmpty()) {
            return emptyAnimal;
        }
        
        if (dogList.isEmpty() || catList.isEmpty()) {
            return dogList.isEmpty() ? catList.pop() : dogList.pop();
        }

        int dogOrder = dogList.peek()[0];
        int catOrder = catList.peek()[0];
        return dogOrder < catOrder ? dogList.pop() : catList.pop();
    
        
    }
    
    public int[] dequeueDog() {
        return dogList.isEmpty() ? emptyAnimal : dogList.pop();
    }
    
    public int[] dequeueCat() {
        return catList.isEmpty() ? emptyAnimal : catList.pop();
    }
}

/**
 * Your AnimalShelf object will be instantiated and called as such:
 * AnimalShelf obj = new AnimalShelf();
 * obj.enqueue(animal);
 * int[] param_2 = obj.dequeueAny();
 * int[] param_3 = obj.dequeueDog();
 * int[] param_4 = obj.dequeueCat();
 */
//leetcode submit region end(Prohibit modification and deletion)