Leetcode: Kth Smallest Element in a Sorted Matrix
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. 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 ≤ n2.
Heap: you need to know the row number and column number of that element(so we can create a tuple class here)
public class Solution {
public int kthSmallest(int[][] matrix, int k) {
Comparator<Tuple> comp = new Comparator<Tuple>() {
public int compare(Tuple tp1, Tuple tp2) {
return tp1.val - tp2.val;
}
};
PriorityQueue<Tuple> minHeap = new PriorityQueue<Tuple>(matrix.length, comp);
int res = 0;
for (int row=0; row<matrix.length; row++) {
minHeap.offer(new Tuple(row, 0, matrix[row][0]));
}
for (int i=1; i<=k; i++) {
Tuple tp = minHeap.poll();
if (i == k) {
res = tp.val;
break;
}
if (tp.y < matrix.length-1) minHeap.offer(new Tuple(tp.x, tp.y+1, matrix[tp.x][tp.y+1]));
}
return res;
}
public class Tuple {
int x;
int y;
int val;
public Tuple(int i, int j, int value) {
this.x = i;
this.y = j;
this.val = value;
}
}
}
Binary Search方法:(12/28仔细看了之后觉得没必要深究,有时间再去深究吧)
public class Solution {
public int kthSmallest(int[][] matrix, int k) {
int lo = matrix[0][0], hi = matrix[matrix.length - 1][matrix[0].length - 1] + 1;//[lo, hi)
while(lo < hi) {
int mid = lo + (hi - lo) / 2;
int count = 0, j = matrix[0].length - 1;
for(int i = 0; i < matrix.length; i++) {
while(j >= 0 && matrix[i][j] > mid) j--;
count += (j + 1);
}
if(count < k) lo = mid + 1;
else hi = mid;
}
return lo;
}
}
referred tohttps://discuss.leetcode.com/topic/52948/share-my-thoughts-and-clean-java-code/2
Leetcode: Kth Smallest Element in a Sorted Matrix的更多相关文章
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- Leetcode:378. Kth Smallest Element in a Sorted Matrix
题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...
- Kth Smallest Element in a Sorted Matrix -- LeetCode
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix(TOP k 问题)
Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kt ...
- 378. Kth Smallest Element in a Sorted Matrix(大顶堆、小顶堆)
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
随机推荐
- (转)面试题--JAVA中静态块、静态变量加载顺序详解
public class Test { //1.第一步,准备加载类 public static void main(String[] args) { new Test(); //4.第四步,new一个 ...
- CentOS_6.5安装Nginx+PHP+MySQL
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # Ch ...
- wsdl 结构
WSDL文档可以分为两部分.分别是抽象部分和具体描述 部分. 抽象部分 抽象部分以独立于平台和语言的方式定义SOAP消息,它们并不包含任何随 机器或语言而变的元素.<types>.< ...
- nrf51822裸机教程-UART
art硬件模块通常都有内置的硬件接收buff,比如51822的硬件uart模块图如下 因为通常接收到uart数据时都会做一些处理.比如保存到数据,或者对数据做一些判断之类的. 如果uart的波特率设置 ...
- 【Android测试】【第九节】MonkeyRunner—— 初识
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4836815.html 不得不说两句,过了这么久才再次更新博 ...
- 转:ASP.NET MVC利用TryUpdateModel来做资料更新 (二)
前言 第一篇說明了 TryUpdateModel 的簡單的應用,除了可指定更新的欄位之外也可排除更新特定的欄位,而因為可搭配 Metadata 做欄位驗證為資料又做了一層把關,但在 ASP.NET M ...
- Magento打印(配送单、退款单、发票)时PDF中的乱码问题
我使用Magento1.4.2,在其自带的TTF文件不能很好地解析中文字符,TTF文件的位置在网站根目录下的/lib/LinLibertineFont/中.打印的中文字符都是这样的 解决方法: 1.在 ...
- How To Set Up Apache with a Free Signed SSL Certificate on a VPS
Prerequisites Before we get started, here are the web tools you need for this tutorial: Google Chrom ...
- CS6破解
1) 序列号这里为大家生成了两个,可以通过软件验证:1325-0949-2080-9819-3777-32301325-0160-5283-9851-2671-8951 2) 破解补丁安装时会用到,请 ...
- HBASE架构解析(一)
http://www.blogjava.net/DLevin/archive/2015/08/22/426877.html 前记 公司内部使用的是MapR版本的Hadoop生态系统,因而从MapR的官 ...