2017年4月2日星期日

Quiz 3 -- Princeton Algorithm

Queue with two stacks. Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations.

Hint: If you push elements onto a stack and then pop them all, they appear in reverse order. If you repeat this process, they're now back in order.

enqueue is to push element to stack 1 and dequeue is to pop element from stack 2, if stack 2 is empty then pop all the element from stack 1 and push them into stack 2 first. 


so for enqueue there is only one push operation, for dequeue , there is at most 2n operations , n is the number of element that was enqueued that was not dequeued. so the amortized number of operations for dequeue is at most 2.


Stack with max. Create a data structure that efficiently supports the stack operations (push and pop) and also a return-the-maximum operation. Assume the elements are reals numbers so that you can compare them.

Hint: Use two stacks, one to store all of the items and a second stack to store the maximums.

A maximum heap with linked list recording the insertion sequence.


or a stack with an sorted array to maintain the order ( each push is a binary search to the sorted array)

Java generics. Explain why Java prohibits generic array creation.

Hint: to start, you need to understand that Java arrays are covariant but Java generics are not: that is, 𝚂𝚝𝚛𝚒𝚗𝚐[] is a subtype of 𝙾𝚋𝚓𝚎𝚌𝚝[], but 𝚂𝚝𝚊𝚌𝚔<𝚂𝚝𝚛𝚒𝚗𝚐> is not a subtype of 𝚂𝚝𝚊𝚌𝚔<𝙾𝚋𝚓𝚎𝚌𝚝>.

It's due to type erasure. Java's Generics type parameter is the compile time information. It's erased in runtime, so a List<String> is erased to List. All the generic type of List share the common class List.class. While java's array is different, different type of array has its own class, for example , String[] and Integer[] have two different class. To determine an array's class type, we need the component type which is the type of the array element which is returned by Class.getComponentType method.

2 条评论:

  1. public class Percolation {

    private boolean[][] opened;
    private int top = 0;
    private int bottom;
    private int size;
    private WeightedQuickUnionUF qf;

    /**
    * Creates N-by-N grid, with all sites

    blocked.
    */
    public Percolation(int N) {
    size = N;
    bottom = size * size + 1;
    qf = new WeightedQuickUnionUF(size *

    size + 2);
    opened = new boolean[size][size];
    }

    /**
    * Opens site (row i, column j) if it is not

    already.
    */
    public void open(int i, int j) {
    opened[i - 1][j - 1] = true;
    if (i == 1) {
    qf.union(getQFIndex(i, j), top);
    }
    if (i == size) {
    qf.union(getQFIndex(i, j), bottom);
    }

    if (j > 1 && isOpen(i, j - 1)) {
    qf.union(getQFIndex(i, j),

    getQFIndex(i, j - 1));
    }
    if (j < size && isOpen(i, j + 1)) {
    qf.union(getQFIndex(i, j),

    getQFIndex(i, j + 1));
    }
    if (i > 1 && isOpen(i - 1, j)) {
    qf.union(getQFIndex(i, j),

    getQFIndex(i - 1, j));
    }
    if (i < size && isOpen(i + 1, j)) {
    qf.union(getQFIndex(i, j),

    getQFIndex(i + 1, j));
    }
    }

    /**
    * Is site (row i, column j) open?
    */
    public boolean isOpen(int i, int j) {
    return opened[i - 1][j - 1];
    }

    /**
    * Is site (row i, column j) full?
    */
    public boolean isFull(int i, int j) {
    if (0 < i && i <= size && 0 < j && j <=

    size) {
    return qf.connected(top, getQFIndex

    (i , j));
    } else {
    throw new IndexOutOfBoundsException

    ();
    }
    }

    /**
    * Does the system percolate?
    */
    public boolean percolates() {
    return qf.connected(top, bottom);
    }

    private int getQFIndex(int i, int j) {
    return size * (i - 1) + j;
    }
    }

    回复删除
  2. Can you please provide the C code?

    回复删除