排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix
[抄题]:
在一个排序矩阵中找从小到大的第 k 个整数。
排序矩阵的定义为:每一行递增,每一列也递增。
[思维问题]:
不知道应该怎么加,因为不是一维单调的。
[一句话思路]:
周围两个数给x或y挪一位, 如果hash数组没有就添加到minheap中
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- class Pair(没有参数)中要有数据类型、方法Pair(int x , int y, int val)
- PairComparator 继承了 Comparator<Pair>类 里面自然就是对pair进行比较, 接口<内容物>+名 = new 内部结构名<内容物>。Queue xx = new PQ<Integer>(k,新建pair对象) 0,0,matrix[0][0] 也要新建对象 new Pair
- x坐标用dx表示, 里面只有0,1
- 取出数从i = 1开始,不用从0开始。因为第一个符合条件的pair是(0,0,matrix[0][0])。
- 数组不包括,直接用!hash[x][y]即可
[二刷]:
- class关键字是小写开头
- next_pair新建之后,minheap直接add(next_pair)即可
- dx,dy数组的作用是加一位坐标,因此里面有东西:0 或 1
[三刷]:
- paircomparator类是继承来的,别的地方可以用,所以写class
[四刷]:
[五刷]:
[总结]:
[复杂度]:Time complexity: O(klgk) Space complexity: O(k)
[英文数据结构,为什么不用别的数据结构]:
heap符合每次取顶 丢掉,取到k个为止
[其他解法]:
二分法
[Follow Up]:
[LC给出的题目变变变]:
373. Find K Pairs with Smallest Sums 也是用minheap实现取最小
668. Kth Smallest Number in Multiplication Table 看着像用heap,其实用二分法 玩数学
719. Find K-th Smallest Pair Distance 其实用二分法 玩数学。看来二分法也能用于查找最小的第k个数
public class Solution {
/*
* @param matrix: a matrix of integers
* @param k: An integer
* @return: the kth smallest number in the matrix
*/
public int kthSmallest(int[][] matrix, int k) {
class Pair {
int x,y,val;
public Pair(int x,int y,int val) {
this.x = x;
this.y = y;
this.val = val;
}
};
class PairComparator implements Comparator<Pair>{
public int compare(Pair a,Pair b) {
return a.val - b.val;
}
};
int m = matrix.length;
int n = matrix[0].length;
int[] dx = new int[]{0,1};
int[] dy = new int[]{1,0};
boolean[][] hash = new boolean[m][n];
Queue<Pair> minHeap = new PriorityQueue<Pair>(k,new PairComparator());
minHeap.add(new Pair(0,0,matrix[0][0]));
for (int i = 1; i < k; i++) {
Pair cur = minHeap.poll();
for (int j = 0; j < 2; j++) {
int next_x = cur.x + dx[j];
int next_y = cur.y + dy[j];
Pair next_Pair = new Pair(next_x,next_y,0);
if (next_x < m && next_y < n && !hash[next_x][next_y]) {
next_Pair.val = matrix[next_x][next_y];
hash[next_x][next_y] = true;
minHeap.add(next_Pair);
}
}
}
return minHeap.peek().val;
}
}
排序矩阵中的从小到大第k个数 · Kth Smallest Number In Sorted Matrix的更多相关文章
- lintcode-401-排序矩阵中的从小到大第k个数
401-排序矩阵中的从小到大第k个数 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. 样例 给出 k = 4 和一个排序矩阵: [ [1 ,5 ,7], [ ...
- [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 ...
- 找到排序矩阵中从小到大第K个数字
一 题目描述 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. 二 题解 由于排序矩阵中的每一行都是递增的,并且每一列都是递增的.从小到大第k个数,实际上就是 ...
- python 判断矩阵中每行非零个数的方法
python 判断矩阵中每行非零个数的方法: # -*- coding: utf-8 -*- # @Time : 2018/5/17 15:05 # @Author : Sizer # @Site : ...
- [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 ...
- LeetCode1337矩阵中最弱的K行
题目 给你一个大小为 m * n 的矩阵 mat,矩阵由若干军人和平民组成,分别用 1 和 0 表示. 请你返回矩阵中战斗力最弱的 k 行的索引,按从最弱到最强排序. 如果第 i 行的军人数量少于第 ...
- 找出一堆数中最小的前K个数
描写叙述: 给定一个整数数组.让你从该数组中找出最小的K个数 思路: 最简洁粗暴的方法就是将该数组进行排序,然后取最前面的K个数就可以. 可是,本题要求的仅仅是求出最小的k个数就可以,用排序能够但显然 ...
- matlab求一个矩阵中各元素出现的个数(归一化)
function [m,n] = stamatrix(a) %网上找到的方法,感觉很巧妙 x=a(:); x=sort(x); d=diff([x;max(x)+1]); count = diff(f ...
随机推荐
- 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
- 第3章 文件I/O(2)_文件I/O系统调用及文件描述符
2. 文件I/O系统调用及文件描述符 2.1 文件I/O系统调用 (1)主要函数 函数 功能 函数 功能 open() 打开文件 read() 读取文件 creat() 创建文件 write() 写入 ...
- Spring Batch批处理以及编程模型
1.批处理: 类似于SQL里面的批处理提交 2.场景: 业务定时进行批处理操作,但是批处理的编程模型是怎么的呢? 3.开源框架 Spring Batch 4.编程模型: reader-processo ...
- Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本)
Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本) Hive 安装依赖 Hadoop 的集群,它是运行在 Hadoop 的基础上. 所以在安装 Hive 之 ...
- SQL Server2016 配置管理器
SQL Server2016 以后版本配置管理器的配置管理器不再同数据库工具集成,是单独的应用. Windows 10: 要打开 SQL Server 配置管理器,请在“起始页”中键入 SQLServ ...
- Noip2011Mayan游戏
题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定 ...
- JS监听浏览器事件
Onunload与Onbeforeunload Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或 ...
- 0_Simple__simplePrintf
在设备代码中使用函数 printf(),没有新的认识. ▶ 源代码 #include <stdio.h> #include <cuda_runtime.h> #include ...
- Apache Maven 入门篇
2017-11-09注释:IntelliJ IDEA 2017.2.5 x64 等新版本会安装maven,为了有好的体验 建议在安装目录找到IntelliJ IDEA 2017.2.5\plugins ...
- mysql 5.7新特新
从 MySQL 5.7.8 开始,MySQL 支持原生的 JSON 数据类型. 创建 JSON 类似 varchar,设置 JSON 主要将字段的 type 是 json, 不能设置长度,可以是 NU ...