[Algo] 26. Kth Smallest Number In Sorted Matrix
Given a matrix of size N x M. For each row the elements are sorted in ascending order, and for each column the elements are also sorted in ascending order. Find the Kth smallest number in it.
Assumptions
- the matrix is not null, N > 0 and M > 0
- K > 0 and K <= N * M
Examples
{ {1, 3, 5, 7},
{2, 4, 8, 9},
{3, 5, 11, 15},
{6, 8, 13, 18} }
- the 5th smallest number is 4
- the 8th smallest number is 6
public class Solution {
public int kthSmallest(int[][] matrix, int k) {
// Write your solution here
int row = matrix.length;
int col = matrix[0].length;
boolean[][] isVisited = new boolean[row][col];
PriorityQueue<Cell> pq = new PriorityQueue<>(k, new Comparator<Cell>() {
@Override
public int compare(Cell a, Cell b) {
return a.value - b.value;
}
});
pq.offer(new Cell(0, 0, matrix[0][0]));
isVisited[0][0] = true;
for (int i = 0; i < k - 1; i++) {
Cell cur = pq.poll();
int curRow = cur.row;
int curCol = cur.col;
if (curRow + 1 < row && !isVisited[curRow + 1][curCol]) {
pq.offer(new Cell(curRow + 1, curCol, matrix[curRow + 1][curCol]));
isVisited[curRow + 1][curCol] = true;
}
if (curCol + 1 < col && !isVisited[curRow][curCol + 1]) {
pq.offer(new Cell(curRow, curCol + 1, matrix[curRow][curCol + 1]));
isVisited[curRow][curCol + 1] = true;
}
}
return pq.poll().value;
}
} class Cell {
int row;
int col;
int value;
public Cell(int row, int col, int value) {
this.row = row;
this.col = col;
this.value = value;
}
}
[Algo] 26. Kth Smallest Number In Sorted Matrix的更多相关文章
- [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字
Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...
- Lintcode: Kth Smallest Number in Sorted Matrix
Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...
- Kth Smallest Number in Sorted Matrix
Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...
- 排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix
[抄题]: 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. [思维问题]: 不知道应该怎么加,因为不是一维单调的. [一句话思路]: 周围两个数给x或y挪一 ...
- Lintcode401 Kth Smallest Number in Sorted Matrix solution 题解
[题目描述] Find the kth smallest number in at row and column sorted matrix. 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的 ...
- [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...
- [Swift]LeetCode668. 乘法表中第k小的数 | Kth Smallest Number in Multiplication Table
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...
- 668. Kth Smallest Number in Multiplication Table
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...
- LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)
题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ 668. Kth S ...
随机推荐
- jQuery实现轮播图--入门
jQuery是一个前台的框架. 主要函数: setInterval 语法:setInterval(code,millisec[,"lang"]) cdoe:需要执行的代码或者要调用 ...
- 题解 Luogu P2499: [SDOI2012]象棋
关于这道题, 我们可以发现移动顺序不会改变答案, 具体来说, 我们有以下引理成立: 对于一个移动过程中的任意一个移动, 若其到达的位置上有一个棋子, 则该方案要么不能将所有棋子移动到最终位置, 要么可 ...
- jquery的读、写、增、删、查方法
# 注:jquery需要导包 格式<script type="text/javascript" src="jquery-3.2.1.js">> ...
- 洛谷 P1018乘积最大
题目描述 今年是国际数学联盟确定的“20002000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰9090周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友 ...
- python + selenium +win32gui + winspy 实现图片上传
过程:模拟点击上传按钮,打开Windows对话框,编辑栏输入文件路径(或网址)点击确定.网上随便找了一个进行测试. 点击后出现Windows上传对话框 用 winspy 来检测窗口的句柄 python ...
- python交互图
花了时间, 记录一下 # -*- coding:utf-8 -*- import matplotlib.pyplot as plt from matplotlib.patches import Rec ...
- PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- CSS 选择器权重计算规则(转)
其实,CSS有自己的优先级计算公式,而不仅仅是行间>内部>外部样式:ID>class>元素. 一.样式类型 1.行间 <h1 style="font-size: ...
- [腾讯 TMQ] 接口测试用例设计
接口测试 [腾讯 TMQ] 接口测试用例设计 腾讯移动品质中心 · 2018年01月17日 · 最后由 于静 回复于 20 天前 · 21794 次阅读 本帖已被设为精华帖! 目录 作者:刘燕 团队: ...
- 4. 现代 javascript class 专题 和 异步专题
class 专题 定义 class //es5 类的定义 属性定义在 function 上, 方法定义在原型链上 function foobar(){ this.foo_ = 'foo'; this ...