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

#! /usr/bin/evn python
#coding:utf-8

from heapq import heappush, heappop

class MinQ(object):
    def __init__(self):
        self.h = []

    def push(self, value):
        heappush(self.h, value)

    def pop(self):
        return heappop(self.h)

class Solution(object):
    def kthSmallest(self, matrix, k):
        """
        :type matrix: List[List[int]]
        :type k: int
        :rtype: int
        """
        assert matrix
        rows_cnt = len(matrix)
        min_q = MinQ()
        for j,c in enumerate(matrix[0]):
            min_q.push([c, 0, j])
        n = 1
        val = None
        while n <= k:
            val = min_q.pop()
            i,j = val[1]+1, val[2]
            if i < rows_cnt:
                min_q.push([matrix[i][j], i, j])
            n += 1
        return val[0]
            

骚年,牢记最小堆的实现!

时间: 2024-10-16 12:19:44

leetcode 378. Kth Smallest Element in a Sorted Matrix的相关文章

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

【Leetcode】Kth Smallest Element in a Sorted Matrix

题目链接:https://leetcode.com/problems/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 smallest element in the matrix. Note that it is the kth smallest eleme

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 kth smallest element in the matrix. Example: matrix = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ], k = 8, return 13. 分析:采用优先队列,自定义比较规则,使得最小元素排在队首. 代码:

378. Kth Smallest Element in a Sorted Matrix

https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/#/solutions http://www.cnblogs.com/EdwardLiu/p/6109080.html Heap : public class Solution { public int kthSmallest(int[][] matrix, int k) { int n = matrix.length; PriorityQueue<Tupl

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

给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [   [ 1,  5,  9],   [10, 11, 13],   [12, 13, 15]],k = 8,返回 13.说明:你可以假设 k 的值永远是有效的, 1 ≤ k ≤ n2 .详见:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/des

[LeetCode] 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 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

Kth Smallest Element in a Sorted Matrix -- LeetCode

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

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

[email&#160;protected] find kth smallest element in two sorted arrays (O(log n time)

The trivial way, O(m + n): Merge both arrays and the k-th smallest element could be accessed directly. Merging would require extra space of O(m+n). The linear run time is pretty good, but could we improve it even further? A better way, O(k): There is