Skip to content

378. Kth Smallest Element in a Sorted Matrix

Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kth smallest element in the matrix.

Note that it is the kth smallest element in the sorted order, not the kth distinct element.

You must find a solution with a memory complexity better than O(n^2).

Example 1:

Input: matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
Output: 13
Explanation: The elements in the matrix are [1,5,9,10,11,12,13,13,15], and the 8th smallest number is 13

Example 2:

Input: matrix = [[-5]], k = 1
Output: -5

Solution:

class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        PriorityQueue<int[]> pq = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]);

        int n = matrix.length;
        for (int i = 0; i < n; i++){
            pq.offer(new int[]{matrix[i][0], i ,0});
            //  value, row, col
        }

        for (int i = 0; i < k - 1; i++){
            int[] cur = pq.poll();
            if (cur[2] != n - 1){ // last col
                pq.offer(new int[]{matrix[cur[1]][cur[2] + 1], cur[1], cur[2] + 1});
                // x, y + 1,             x . y + 1
            }
        }

        return pq.poll()[0];

    }
}

// TC: O(klogn)
// SC: O(n)
class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>(){
            public int compare(int[] a, int[] b){
                return a[0] - b[0];
            }
        });

        int n = matrix.length;
        for (int i = 0; i < n; i++){
            pq.offer(new int[]{matrix[i][0], i ,0});
            //  value, row, col
        }

        for (int i = 0; i < k - 1; i++){
            int[] cur = pq.poll();
            if (cur[2] != n - 1){ // last col
                pq.offer(new int[]{matrix[cur[1]][cur[2] + 1], cur[1], cur[2] + 1});
                // x, y + 1,             x . y + 1
            }
        }

        return pq.poll()[0];

    }
}

// TC: O(klogn)
// SC: O(n)
class Solution {
    static class Cell{
        int row;
        int col;
        int value;

        Cell(int row, int col, int value){
            this.row = row;
            this.col = col;
            this.value = value;
        }
    }
    public int kthSmallest(int[][] matrix, int k) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        boolean[][] visited = new boolean[rows][cols];

        PriorityQueue<Cell> minHeap = new PriorityQueue<Cell>(k, new Comparator<Cell>(){
            @Override
            public int compare(Cell c1, Cell c2){
                if (c1.value == c2.value){
                    return 0;
                }else if (c1.value <  c2.value){
                    return -1;
                }else {
                    return 1;
                }
            }
        });

        minHeap.offer(new Cell(0, 0, matrix[0][0]));
        visited[0][0] = true;

        for (int i = 1; i < k; i++){
            Cell cur = minHeap.poll();

            if (cur.row + 1 < rows && !visited[cur.row + 1][cur.col]){
                minHeap.offer(new Cell(cur.row + 1, cur.col, matrix[cur.row+1][cur.col]));
                visited[cur.row+ 1][cur.col] = true ;
            }

            if (cur.col + 1 < cols && !visited[cur.row][cur.col+1]){
                minHeap.offer(new Cell(cur.row, cur.col +1, matrix[cur.row][cur.col+1]));
                visited[cur.row][cur.col+1] = true ;
            }

        }

        return minHeap.peek().value;

    }
}


// TC: O(nlogn)

// SC: O(n)
class Solution {
    public static int kthSmallest(int[][] matrix, int k){
        int n = matrix.length; // 3 //
        int m = matrix[0].length; // 3
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){
                pq.offer(matrix[i][j]);
            }
        }

        for (int i = 0; i < k - 1; i++){
            pq.poll();
        }

        return pq.poll();
    }
}

// TC: O(n^2logn)
// SC: O(n^2)