leetcode :Binary Search:Kth Smallest Element in a Sorted Matrix(378)

原创 2016年08月30日 14:46:30

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.

matrix = [
   [ 1,  5,  9],
   [10, 11, 13],
   [12, 13, 15]
],
k = 8,

return 13.

class Solution {
public:
    int kthSmallest(vector<vector<int>>& matrix, int k) {
        priority_queue<int, vector<int>> q;
        for (int i = 0; i < matrix.size(); ++i) {
            for (int j = 0; j < matrix[i].size(); ++j) {
                q.emplace(matrix[i][j]);
                if (q.size() > k) q.pop();
            }
        }
        return q.top();
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

378. Kth Smallest Element in a Sorted Matrix leetcode binary_search 378

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kt...

378. Kth Smallest Element in a Sorted Matrix-binary search/heap

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 解题报告

LeetCode 378. Kth Smallest Element in a Sorted Matrix 解题报告

leetCode No.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 kt...

[leetcode] 378. Kth Smallest Element in a Sorted Matrix 解题报告

题目链接: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ Given a n x n matrix ...

LeetCode笔记:378. Kth Smallest Element in a Sorted Matrix

在行列分别升序的矩阵中找第 k 小的元素

LeetCode:378 Kth Smallest Element in a Sorted Matrix

378. Kth Smallest Element in a Sorted MatrixGiven a n x n matrix where each of the rows and columns ...
  • Wisimer
  • Wisimer
  • 2016年08月13日 23:33
  • 205

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

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

leetcode 378. Kth Smallest Element in a Sorted Matrix

leetcode 378 Kth Smallest Element in a Sorted Matrix
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode :Binary Search:Kth Smallest Element in a Sorted Matrix(378)
举报原因:
原因补充:

(最多只允许输入30个字)