I thought it is too easy?


  • 0
    B

    This is my solution, but only 19 cases passed, could anyone tell me what scenarios it will go wrong?
    ...
    public class MedianFinder {

    boolean odd;
    int medianIndex = -1;
    ArrayList data = new ArrayList();
    // Adds a number into the data structure.
    public void addNum(int num) {
        data.add(num);
        odd = !odd;
        if (odd) {
            medianIndex++;
        }
    }
    
    // Returns the median of current data stream
    public double findMedian() {
        if (odd) {
            return (int)data.get(medianIndex) * 1.0;
        }else {
            return ((int)data.get(medianIndex) + (int)data.get(medianIndex+1))/2.0;
        }
    }
    

    };
    ...


  • 0
    R

    what if the data stream is unsorted


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.