Run difference between Leetcode and Netbeans


  • 0
    N

    I first came up with a simple solution, and it should be correct, but in leetcode the result was sometimes wrong. I used Netbeans IDE to debug it. However, I always got the correct answer in Netbeans, i.e. the code has no bugs.

    Test case:
    [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15]]
    6

    Could anybody help me check what is wrong? Thanks!

    The code is below:
    It just starts from the top left, put the matrix[0][0] into the "border" HashSet, then expand the border at the smallest element in the border HashSet, while counting the how many smallest number has been encountered. Return the kth small one.
    '''

    public int kthSmallest(int[][] matrix, int k) {
        if (matrix.length == 1) {
            return matrix[0][0];
        }
        Set<Point> border = new HashSet<>();
    
        int n = matrix.length, res = matrix[0][0];
        Point cur = new Point(0, 0);
        border.add(cur);
    
        while (k > 0) {
            res = Integer.MAX_VALUE;
            for (Point p : border) {
                if (res > matrix[p.x][p.y]) {
                    res = matrix[p.x][p.y];
                    cur = p;
                }
            }
            if (cur.x + 1 < n) {
                border.add(new Point(cur.x + 1, cur.y));
            }
            if (cur.y + 1 < n) {
                border.add(new Point(cur.x, cur.y + 1));
            }
            border.remove(cur);
            k--;
        }
        return res;
    }
    

    '''


Log in to reply
 

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