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. P1970 花匠

    状态定义是dp中非常重要的,可以直接影响到效率,如此题,第一种思路是: #include <bits/stdc++.h> using namespace std; const int ma ...

  2. 后半部分样式和JS前半部分脚本语言

    样式 剩余样式: 1.<div style=display:"none"></div>:nono 是隐藏该元素内容,block是显示该元素内容 2.< ...

  3. BLE链路层状态机

    BLE的Link层,应当是了解BLE需要首先熟悉的一部分,BLE的Controller部分主要都在围绕这一部分实现的.Link层的内容规定了BLE底层是怎么实现蓝牙设备之间的控制,数据传输等等的.Li ...

  4. URL编码数据转换为JSON数据

    NSString *urlString; urlString=[self    URLDecodedString:urlString]; -(NSString *)URLDecodedString:( ...

  5. vs2013 RTM 激活码

    BWG7X-J98B3-W34RT-33B3R-JVYW9

  6. 防止sql注入,过滤敏感关键字

    //sql过滤关键字 public static bool CheckKeyWord(string sWord) { //过滤关键字 string StrKeyWord = @"select ...

  7. C++ 字符串操作常见函数

    //字符串拷贝,排除指定字符 char *strcpy_exclude_char(char *dst, const int dst_len, const char *src, const char * ...

  8. VS2015 多项目源码共享链接

    Eclipse有这个功能,在一个项目中加入另一个项目文件夹的引用,源码包含过来,这样不必copy一份代码,只需要维护一份源代码.一直想在VS中找到这个功能,目前项目需要,终于google到了. htt ...

  9. Marriage Match IV---hdu3416(spfa + Dinic)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3416 有一个有向图,n个点,m条边,给一个起点和终点,求出从起点到终点的最短路共有几条,每 ...

  10. UITableViewCell 设置圆角

    #import <QuartzCore/QuartzCore.h> QuartzCore.framework [self.commentsCell.layer setMasksToBoun ...