【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
标签: LeetCode
题目地址:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/
题目描述:
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.
题目大意
找到一个有序二维数组的第k小的数字。这个二维数组,每行依次增加,每列依次增加。
解题方法
看到第k个的问题,就想到了用堆去做。竟然没超时!下面的代码也能通过。
class Solution(object):
def kthSmallest(self, matrix, k):
"""
:type matrix: List[List[int]]
:type k: int
:rtype: int
"""
nums = []
for line in matrix:
nums.extend(line)
heapq.heapify(nums)
res = 0
for i in range(k):
res = heapq.heappop(nums)
return res
根据堆的算法,可以实现改进,不用把每个元素都进堆,只需要把“最可能是最小值”的进堆即可。也就是说我们每次进堆的元素只要包括下一个最小的元素即可。
我们把左上角最小的元素和其索引进堆,然后把堆里的元素进行一个遍历,每次把元素的右边的元素进堆。当是第一列元素的时候,把这个元素下面的元素也进堆。
class Solution(object):
def kthSmallest(self, matrix, k):
"""
:type matrix: List[List[int]]
:type k: int
:rtype: int
"""
m, n = len(matrix), len(matrix[0])
q = [(matrix[0][0], 0, 0)]
ans = 0
for _ in range(k):
ans, i, j = heapq.heappop(q)
if j == 0 and i + 1 < m:
heapq.heappush(q, (matrix[i + 1][j], i + 1, j))
if j + 1 < n:
heapq.heappush(q, (matrix[i][j + 1], i, j + 1))
return ans
方法二:
二分查找,留给二刷。
http://bookshadow.com/weblog/2016/08/01/leetcode-kth-smallest-element-in-a-sorted-matrix/
http://www.cnblogs.com/grandyang/p/5727892.html
日期
2018 年 3 月 15 日 –雾霾消散,春光明媚
【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)的更多相关文章
- [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 ...
- 【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 ...
- 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 ...
- 378. Kth Smallest Element in a Sorted Matrix(java,优先队列)
题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...
- 378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [ [ 1, 5, 9], [ ...
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
随机推荐
- 一站式Flink&Spark平台解决方案——StreamX
大家好,我是独孤风.今天为大家推荐的是一个完全开源的项目StreamX.该项目的发起者Ben也是我的好朋友. ****什么是StreamX,StreamX 是Flink & Spark极速开发 ...
- 巩固javaweb第一天
巩固内容: 实例解析 <!DOCTYPE html> 声明为 HTML5 文档 <html> 元素是 HTML 页面的根元素 <head> 元素包含了文档的元(me ...
- d3入门二-常用 方法
CSV 版本6.5.0 这里的data实际上是csv中的一行数据 d3.csv("static/data/dept_cpu.csv",function (data) { conso ...
- Output of C++ Program | Set 8
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- 【Linux】【Services】【Docker】基础理论
1. 名称空间:NameSpace 内核级别,环境隔离: 1.1. 名称空间的历史 PID NameSpace:Linux 2.6.24 ,PID隔离 Network NameSpace:Linux ...
- 小程序的事件 bindtap bindinput
一.bindtap事件 在wxml文件里绑定: <view class='wel-list' bindtap='TZdown'> <image src="/images/w ...
- B树和B+树原理图文解析
B树与B+树不同的地方在于插入是从底向上进行(当然查找与二叉树相同,都是从上往下) 二者都通常用于数据库和操作系统的文件系统中,非关系型数据库索引如mongoDB用的B树,大部分关系型数据库索引使用的 ...
- java 整型
byte(1字节).short(2字节).int(4字节).long(16字节) java中前缀加上0b或者0B就可以写二进制数,前缀加上0就可以写八进制数,前缀加上0x或者0X就可以写十六进制数 一 ...
- EFK的安装和收集docker容器日志展示
在用户根目录创建个filebeat.docker.yml文件,文件内容如下 filebeat.config: modules: path: ${path.config}/modules.d/*.yml ...
- Nginx HTTP块配置
1 配置块的嵌套 http { upstream {...} split_clients {...} map {...} geo {...} server { if () {...} location ...