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的更多相关文章

  1. [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 ...

  2. LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13

    378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...

  3. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  4. [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 ...

  5. 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 ...

  6. 【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 ...

  7. 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 ...

  8. 【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 ...

  9. 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 ...

随机推荐

  1. NSQ部署

    一.      简介 NSQ主要有三个主要程序和一个Web服务程序: nsqd:是守护进程,接收,缓存,并投递消息给客户端 nsqlookupd:是一个守护进程,为消费者提供运行时发现服务,来查找指定 ...

  2. 策略模式代替大量的if else

    原代码 public class Example { public Double calRecharge(Double charge, RechargeTypeEnum type) { if (typ ...

  3. ajax例子:审核验证用户名;登录界面

    审核验证用户名主页面: <body><div>用户名:<input type="text" id="uid" /><s ...

  4. Android源码剖析之Framework层进阶版(Wms窗口管理)

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 上一篇我们主要讲了Ams,篇幅有限,本篇再讲讲Wms,即WindowManagerService,管 ...

  5. 【Android开发学习笔记】【第七课】五大布局-上

    概念 Android程序各式各样,依靠的就是布局,先来看看布局都是怎么来的: 白色部分就是我们经常用的几种布局,主要说说介绍下面五大布局 FrameLayout AbsoluteLayout Line ...

  6. 【Java 基础篇】【第七课】组合

    我所理解的组合就是在一个类当中又包含了另一个类的对象. 这样的方式就是组合吧: 电池是一个类,有电量 手电筒需要电池 看代码吧: // 电池类 class Battery { // 充电 public ...

  7. 设计模式:适配器模式(Adapter)

    定  义:将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 有些国家用110V电压,而我们国家用的是220V,但是我们的电器,比 ...

  8. node express 学习1

    参考链接https://cnodejs.org/topic/55ece31004e2cdb230671c50 express-session connect-nongo mongoose 1.安装mo ...

  9. Android笔记:真机调试

    参考:android通过USB使用真机调试程序 手机:华为Y511-U00 1.将手机USB调试模式连接到PC(直接用数据线连接到PC一般可以自动切换) 2.连接成功后提示安装驱动,我选择的是自动安装 ...

  10. php保存远程文件到本地的方法

    用到了ob_start();<?php header("Content-type:text/html charset=utf-8"); if(!empty($_POST['p ...