# 378     Kth Smallest Element in a Sorted Matrix

## 378. [Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/)

## 1. Question

Given anxnmatrix where each of the rows and columns are sorted in ascending order, find 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:**

```
matrix = [
   [ 1,  5,  9],
   [10, 11, 13],
   [12, 13, 15]
],
k = 8,

return 13.
```

**Note:**\
You may assume k is always valid, 1 ≤ k ≤ n^2.

## 2. Implementation

**(1) Binary Search**

思路: 为什么二分法的结果是正确的呢？ 首先在二分法，我们知道要第k小的数一定在矩阵里，也即start <= res <= end, 而二分结束的条件是start >= end, 所以start == right == res。

```java
class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        if (matrix == null || matrix.length == 0 || k < 0) {
            return -1;
        }

        int m = matrix.length, n = matrix[0].length;

        int start = matrix[0][0], end = matrix[m - 1][n - 1], mid = 0;
        int count = 0;

        while (start < end) {
            mid = start + (end - start) / 2;
            count = searchAndCount(mid, matrix);

            if (count < k) {
                start = mid + 1;
            }
            else {
                end = mid;
            }
        }

        return start;
    }

    public int searchAndCount(int num, int[][] matrix) {
        int row = matrix.length - 1, col = 0, count = 0;

        while (row >= 0 && col < matrix[0].length) {
            if (matrix[row][col] <= num) {
                count += (row + 1);
                ++col;
            }
            else {
                --row;
            }
        }
        return count;
    }
}
```

**(2) Heap**

```java
class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        int n = matrix.length;
        PriorityQueue<CellInfo> minHeap = new PriorityQueue<>();

        for (int j = 0; j < n; j++) {
            minHeap.add(new CellInfo(0, j, matrix[0][j]));
        }

        for (int i = 1; i < Math.min(k, n * n); i++) {
            CellInfo curCell = minHeap.remove();

            int curRow = curCell.row;
            int curCol = curCell.col;

            if (curRow + 1 < n) {
                minHeap.add(new CellInfo(curRow + 1, curCol, matrix[curRow + 1][curCol]));
            }
        }
        return minHeap.peek().val;
    }

    class CellInfo implements Comparable<CellInfo> {
        int row;
        int col;
        int val;

        public CellInfo(int row, int col, int val) {
            this.row = row;
            this.col = col;
            this.val = val;
        }

        public int compareTo(CellInfo that) {
            return this.val - that.val;
        }
    }
}
```

## 3. Time & Space Complexity

**Binary Search:** 时间复杂度O((m + n)log(S)), 其中S指的是search space,即矩阵中最小数到最大数的区间, 空间复杂度O(1)

**Heap**: 时间复杂度O(n^2 \* logn), 空间复杂度O(n), heap最多存储n(列数)个matrix的元素
