【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:

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

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

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

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

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

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

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

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

  8. 378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [   [ 1,  5,  9],   [ ...

  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 矩阵,其中每行和每列元素均按升序排序,找到矩 ...

随机推荐

  1. linux中对errno是EINTR的处理

    慢系统调用(slow system call):此术语适用于那些可能永远阻塞的系统调用.永远阻塞的系统调用是指调用有可能永远无法返回,多数网络支持函数都属于这一类.如:若没有客户连接到服务器上,那么服 ...

  2. Linux 中的五种 IO 模型

    Linux 中的五种 IO 模型 在正式开始讲Linux IO模型前,比如:同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一 ...

  3. 在Idea上用JDBC连接mysql数据库

    一.前言 本次操作建立在idea中java环境已配置的基础上 二.操作步骤 1.建立Web项目后,添加驱动包 mysql-connector-java-5.0.8-bin.jar (1)下载mysql ...

  4. day06 视图层

    day06 视图层 今日内容 视图层 小白必会三板斧 JsonResponse form表单发送文件 FBV与CBV FBV基于函数的视图 CBV基于类的视图 模板层 模板语法的传值 模板语法之过滤器 ...

  5. 2016广东工业大学新生杯决赛 A-pigofzhou的巧克力棒

    题目:GDUTOJ | pigofzhou的巧克力棒 (gdutcode.cn) 之前看了大佬博客的题解,一直没懂(我太菜了),后来听了朋友@77的讲解,终于懂了. 和拆分出2的n次方不一样,这是一种 ...

  6. 案例 stm32单片机,adc的双通道+dma 内部温度

    可以这样理解 先配置adc :有几个通道就配置几个通道. 然后配置dma,dma是针对adc的,而不是针对通道的. 一开始我以为一个adc通道对应一个dma通道.(这里是错的,其实是我想复杂了) 一个 ...

  7. Linux学习 - 输入输出重定向,管道符,通配符

    一.键盘输入读取read read [选项] [变量名] -p [显示信息] 在等待read输入时,输出提示信息 -t [秒数] 指定read输入等待时间 -n [字符数] 指定read只接收n个字符 ...

  8. binlog2sql 解析日志失败 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte

    python35 ./binlog2sql.py -h... -P... -u... -p... -B --start-file="mysql-bin.091940" --star ...

  9. DBMS_RANDOM包详解

    DBMS_RAMDOM包中一共包含9个存储过程和函数,其中6个是现在用的,3个是已经过时的: 当前版本11gR2 每次生成一个随机数oracle都会初始化一个种子,也可以调用seed过程自己初始化一个 ...

  10. django搭建示例-ubantu环境

    python3安装--------------------------------------------------------------------------- 最新的django依赖pyth ...