2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_378.java 1.40 KB
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-10 21:34 . 优化题解
package com.hit.basmath.interview.top_interview_questions.hard_collection.sorting_and_searching;
/**
* 378. Kth Smallest Element in a Sorted Matrix
* <p>
* Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
* <p>
* Note that it is the kth smallest element in the sorted order, not the kth distinct element.
* <p>
* Example:
* <p>
* matrix = [
* [ 1, 5, 9],
* [10, 11, 13],
* [12, 13, 15]
* ],
* k = 8,
* <p>
* return 13.
* <p>
* Note:
* <p>
* You may assume k is always valid, 1 ≤ k ≤ n^2.
*/
public class _378 {
public int kthSmallest(int[][] matrix, int k) {
int length = matrix.length;
int low = matrix[0][0], high = matrix[length - 1][length - 1];
while (low <= high) {
int mid = low + (high - low) / 2;
int count = getLessEqual(matrix, mid);
if (count < k) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return low;
}
private int getLessEqual(int[][] matrix, int val) {
int ans = 0;
int length = matrix.length, i = length - 1, j = 0;
while (i >= 0 && j < length) {
if (matrix[i][j] > val) {
i--;
} else {
ans += i + 1;
j++;
}
}
return ans;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助