Skip to content

Latest commit

 

History

History

378

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Given an n x n matrix where each of the rows and columns are 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.

 

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

 

Constraints:

  • n == matrix.length
  • n == matrix[i].length
  • 1 <= n <= 300
  • -109 <= matrix[i][j] <= 109
  • All the rows and columns of matrix are guaranteed to be sorted in non-decreasing order.
  • 1 <= k <= n2

Companies:
Amazon, Facebook, Apple

Related Topics:
Array, Binary Search, Sorting, Heap (Priority Queue), Matrix

Similar Questions:

Solution 1. BFS + Heap

// OJ: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
// Author: github.com/lzl124631x
// Time: O(Klog(N))
// Space: O(N)
class Solution {
    typedef tuple<int, int, int> Item;
public:
    int kthSmallest(vector<vector<int>>& A, int k) {
        int N = A.size(), dirs[2][2] = {{0,1},{1,0}};
        vector<vector<bool>> seen(N, vector<bool>(N));
        priority_queue<Item, vector<Item>, greater<>> pq;
        pq.emplace(A[0][0], 0, 0);
        seen[0][0] = true;
        while (--k) {
            auto [n, x, y] = pq.top();
            pq.pop();
            for (auto &[dx, dy] : dirs) {
                int a = x + dx, b = y + dy;
                if (a < 0 || a >= N || b < 0 || b >= N || seen[a][b]) continue;
                seen[a][b] = true;
                pq.emplace(A[a][b], a, b);
            }
        }
        return get<0>(pq.top());
    }
};

Solution 2. BFS + HEAP

// OJ: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
// Author: github.com/lzl124631x
// Time: O(KlogN)
// Space: O(N)
class Solution {
    typedef tuple<int, int, int> Item;
public:
    int kthSmallest(vector<vector<int>>& A, int k) {
        int N = A.size();
        priority_queue<Item, vector<Item>, greater<>> pq;
        for (int i = 0; i < N; ++i) pq.emplace(A[0][i], 0, i);
        while (--k) {
            auto [n, x, y] = pq.top();
            pq.pop();
            if (x + 1 < N) pq.emplace(A[x + 1][y], x + 1, y);
        }
        return get<0>(pq.top());
    }
};

Solution 3. Binary Search

// OJ: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
// Author: github.com/lzl124631x
// Time: O(Nlog(max(A)))
// Space: O(1)
class Solution {
    int rank(vector<vector<int>> &A, int num) {
        int N = A.size(), cnt = 0, j = N - 1;
        for (int i = 0; i < N; ++i) {
            while (j >= 0 && A[i][j] > num) --j;
            cnt += j + 1;
        }
        return cnt;
    }
public:
    int kthSmallest(vector<vector<int>>& A, int k) {
        int N = A.size(), L = A[0][0], R = A[N - 1][N - 1];
        while (L < R) {
            int M = (L + R) / 2;
            if (rank(A, M) < k) L = M + 1;
            else R = M;
        }
        return L;
    }
};