Cranking the Coding Interview: Stacks and Queues

Animal Shelter

The topic is that deque is perfect.

However, the implementation of deque can be implemented by list, where the first pointer points to the beginning and the last one points to the end.

Use the first pointer to do the adopted operation, if it does not meet the requirements, then create a list to store the required content. Then when you need to add animals to the shelter, add them to the back of the list.

Specific code implementation:

class CatDogAsylum {
public:
    vector<int> asylum(vector<vector<int> > ope) {
        // write code here
        deque<int> que;
        vector<int> seq;

        int ope_size = ope.size();
        for(int i = 0; i < ope_size; i++) {
            // put animal into shelter
            if(ope[i][0] == 1) {
                que.push_back(ope[i][1]);
            }
            // adopt anmial 
            else {
                vector<int> pre;
                if(ope[i][1] == 0) {
                    seq.push_back(que.front());
                    que.pop_front();
                }
                else {
                    // adopt dog
                    if(ope[i][1] == 1) {
                        while(!que.empty()) {
                            int tp = que.front();
                            if(tp > 0) { seq.push_back(tp); que.pop_front(); break; }
                            else { que.pop_front(); pre.push_back(tp); } 
                        }   
                        if(!pre.empty()) que.insert(que.begin(), pre.begin(), pre.end());
                    } 
                    // adopt cat
                    else {
                        while(!que.empty()) {
                            int tp = que.front();
                            if(tp < 0) { seq.push_back(tp); que.pop_front(); break; }
                            else { que.pop_front(); pre.push_back(tp); } 
                        }   
                        if(!pre.empty()) que.insert(que.begin(), pre.begin(), pre.end());                       
                    }
                }
            }
        }
        return seq;
    }
};

The code on cc189 is as follows:

abstract class Animal {
    private int order;
    protected String name;
    public Animal(String n) {name = n;}
    public int getOrder() {return order;}
    public boolean isOrderThan(Animal a) {
        return this.order < a.getOrder();
    }
};

class AnimalQueue{
    LinkedList<Dog> dogs = new LinkedList<Dog>();
    LinkedList<Cat> cats = new LinkedList<Cats>();
    private int oreder = 0;

    public void enqueue(Animal a) {
        a.setOrder(order);
        order++;
    }

    public Animal dequeueAny() {
        if(dogs.size() == 0) {
            return dequeueCats();
        }
        if(cats.size() == 0) {
            return dequeueDogs();
        }
        Dog dog = dog.peek();
        Cat cat = cats.peek();
        if(dog.isOrderThan(cat)) return deuqueDogs();
        else return dequeueCats();
    } 

    public Dog dequeueDogs() {
        return dogs.poll();
    }

    public Cat dequeueCats() {
        return cats.poll();
    } 
}

public class Dog extend Animal {
    public Dog(String n) {super(n);}
}

public class Cat extend Animal {
    public Cat(String n) {super(n);}
}

Queue via Stacks

What I do here is to implement two stacks as queues. My method is relatively simple, that is to use one stack to store data and another stack to do transit. Then it can be written as follows:

class Solution
{
public:
    void push(int node) {
        if(!stack1.empty()) {
            int tmp;
            while(!stack1.empty()) {
                tmp = stack1.top();
                stack1.pop();
                stack2.push(tmp);
            }
            stack1.push(node);
            while(!stack2.empty()) {
                tmp = stack2.top();
                stack2.pop();
                stack1.push(tmp);
            }
        }   
        else stack1.push(node);
    }

    int pop() {
        if(!stack1.empty()) {int res = stack1.top(); stack1.pop(); return res;}
        else return 0;
    }

private:
    stack<int> stack1;
    stack<int> stack2;
};

Three in One

Posted by jeffkee on Wed, 13 Feb 2019 12:06:18 -0800