Passed in local test, but got wrong answers in OA for the Skyline Problem


  • 0
    H
    I did test my code in local and get the right answer, just wonder who could point my errors in the code.   
    Wrong answer for OA test case [0,1,3] i got [[0,3],[2,0]]. But in local I get the right answer[[0,3].[1,0]]
    

    Here is my code:

    public class Solution {
    private class building
    {
        public int start;
        public int end;
        public int height;
        public building(int x, int y, int z)
        {
            this.start = x;
            this.end = y;
            this.height = z;
        }
    }
    public List<int[]> getSkyline(int[][] buildings) {
        PriorityQueue<building> dict= new PriorityQueue<building>(1, new Comparator<building>()
        {
            public int compare(building a, building b)
            {
                if (a.height < b.height)
                    return 1;
                else
                    return -1;
            }
        });
        List<int[]> result = new ArrayList<int[]>();
        int toplen = 0;
        for (int i = 0; i < buildings.length; ++i)
        {
            for (int j = i + 1; j < buildings[0].length; ++j)
            {
                if (buildings[i][j] == 0)
                    continue;
                else
                {
                    building temp = new building(i,j,buildings[i][j]);
                    dict.add(temp);
                }
            }
            while (dict.size() > 0 && dict.peek().end <= i)
                dict.poll();
            int nowtoplen;
            if (dict.size() == 0)
                nowtoplen = 0;
            else
                nowtoplen = dict.peek().height;
            if (nowtoplen != toplen)
            {
                int[] x = new int[2];
                x[0] = i;
                x[1] = nowtoplen;
                result.add(x);
                toplen = nowtoplen;
            }
        }
        int start = buildings.length;
        while (dict.size() > 0)
        {
            while (dict.size() > 0 && dict.peek().end <= start)
                dict.poll();
            int nowtoplen;
            if (dict.size() == 0)
                nowtoplen = 0;
            else
                nowtoplen = dict.peek().height;
            if (nowtoplen != toplen)
            {
                int[] x = new int[2];
                x[0] = start;
                x[1] = nowtoplen;
                result.add(x);
                toplen = nowtoplen;
            }
            ++start;
        }
        return result;
    }
    

    }


Log in to reply
 

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