【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命令行好玩的命令

    0.cal 2019  #输出日历并显示今天是哪一天 1.命令"date",显示系统的当前日期和时间: 2.命令"date 040100002016",屏幕显示 ...

  2. linux中chage命令的基本使用

    在Linux中chage命令常用于设置系统用户的账户属性 Usage: chage [options] LOGIN Options: -d, --lastday LAST_DAY set date o ...

  3. adhere, adjust, adjacent

    adhere to stick,不是to here. 在古英语里,stick是twig(细树枝).fasten(想必是用twig来固定).后引申为粘住.stick还有stab, pierce的意思,想 ...

  4. Vue API 3 (模板语法 ,指令)

    条件 v-if v-if 指令用于条件性地渲染一块内容.这块内容只会在指令的表达式返回 truthy 值的时候被渲染. v-show v-show 指令也是用于根据条件展示一块内容.v-show 只是 ...

  5. ReactiveCocoa操作方法-重复

    retry重试      只要失败,就会重新执行创建信号中的block,直到成功. __block int i = 0; [[[RACSignal createSignal:^RACDisposabl ...

  6. 类型类 && .class 与 .getClass() 的区别

    一. 什么是类型类 Java 中的每一个类(.java 文件)被编译成 .class 文件的时候,Java虚拟机(JVM)会为这个类生成一个类对象(我们姑且认为就是 .class 文件),这个对象包含 ...

  7. Spring Boot下使用拦截器

    Spring Boot对于原来在配置文件配置的内容,现在全部体现在一个类中,该类需要继承自WebMvcConfigurationSupport类,并使用@Configuration进行注解,表示该类为 ...

  8. JAVA日志发展史

    JAVA日志发展史 第一阶段 2001年以前,Java是没有日志库的,打印日志全凭System.out和System.err 缺点: 产生大量的IO操作同时在生产环境中无法合理的控制是否需要输出 输出 ...

  9. Jenkins安全加固

    1.jenkins未授权访问 描述 jenkins不当配置可导致未授权访问管理控制台,可以通过脚本命令行执行系统命令.通过该漏洞,可以后台管理服务,通过脚本命令行功能执行系统命令,如反弹shell,w ...

  10. 『学了就忘』Linux系统管理 — 84、Linux中进程的管理

    目录 1.Linux系统中的信号 2.杀掉进程的命令 (1)kill命令 (2)killall命令 (3)pkill命令 1.Linux系统中的信号 Linux系统中可以识别的信号较多,我们可以使用命 ...