思路1:

使用堆。

实现:

 class Solution
{
public:
int kthSmallest(vector<vector<int>>& matrix, int k)
{
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<pii>> q;
int n = matrix.size();
vector<int> v(n, );
for (int i = ; i < n; i++) q.push(make_pair(matrix[i][v[i]], i));
pair<int, int> tmp;
for (int i = ; i < k; i++)
{
tmp = q.top(); q.pop();
int id = tmp.second;
while (v[id] == n)
{
tmp = q.top();
q.pop();
id = tmp.second;
}
assert(v[id] != n);
v[id]++;
q.push(make_pair(matrix[id][v[id]], id));
}
return tmp.first;
}
};

思路2:

二分查找最小的x,满足大于x的元素数量不超过n * n - k个。

实现:

 class Solution
{
public:
bool check(vector<vector<int>>& matrix, int x, int g)
{
int n = matrix.size(), cnt = ;
for (int i = ; i < n; i++)
{
int p = upper_bound(matrix[i].begin(), matrix[i].end(), x) - matrix[i].begin();
cnt += n - p;
}
return cnt <= g;
}
int kthSmallest(vector<vector<int>>& matrix, int k)
{
int n = matrix.size();
if (n == ) return matrix[][];
int l = matrix[][], r = matrix[n - ][n - ];
int ans = l;
while (l <= r)
{
int m = l + r >> ;
if (check(matrix, m, n * n - k))
{
ans = m;
r = m - ;
}
else l = m + ;
}
return ans;
}
};

思路3:

由于矩阵每一行和每一列都是递增的,可以在思路2的基础上进行优化。

实现:

 class Solution
{
public:
bool check(vector<vector<int>>& matrix, int x, int g)
{
int n = matrix.size(), j = n - , cnt = ;
for (int i = ; i < n; i++)
{
while (j >= && matrix[i][j] > x) j--; //优化,不必每次都二分
cnt += n - - j;
}
return cnt <= g;
}
int kthSmallest(vector<vector<int>>& matrix, int k)
{
int n = matrix.size();
if (n == ) return matrix[][];
int l = matrix[][], r = matrix[n - ][n - ];
int ans = l;
while (l <= r)
{
int m = l + r >> ;
if (check(matrix, m, n * n - k))
{
ans = m;
r = m - ;
}
else l = m + ;
}
return ans;
}
};

leetcode378 Kth Smallest Element in a Sorted Matrix的更多相关文章

  1. LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13

    378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...

  2. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  3. [Swift]LeetCode378. 有序矩阵中第K小的元素 | 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 ...

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

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

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

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

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

随机推荐

  1. 「USACO16OPEN」「LuoguP3147」262144(区间dp

    P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...

  2. Battle Ships(复习泛化物品**)

    传送门Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which i ...

  3. 【重磅推荐】嵌入式Linux经典书单(部分含视频)

    一直都有人问我要书单,在网上搜索大半天,没找到合适的,他们写的太不负责了,遂决定自己整理. 本书单综合了豆瓣知乎热评,尤其参考了一线开发者韦东山学员群的小伙伴们的意见, 再结合本人多年答疑经验整理而成 ...

  4. MongoDB监控之一:运行状态、性能监控,分析

    为什么要监控? 监控及时获得应用的运行状态信息,在问题出现时及时发现. 监控什么? CPU.内存.磁盘I/O.应用程序(MongoDB).进程监控(ps -aux).错误日志监控 1.4.1 Mong ...

  5. MVC中为自动生成实体类添加验证

    将额外的基于特性的元数据(象验证特性)施加到由VS设计器自动生成/维护的类的一个方法是,采用一个我们称之为“伙伴类(buddy classes)”的技术. 基本上来说,你创建另外一个类,包含你的验证特 ...

  6. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  7. 2016CCPC东北地区大学生程序设计竞赛【01/03/05/06/08】

    吧啦啦啦啦啦啦啦啦啦啦啦能量,ACM,跨!变身!变成一个智障! 04正在酝酿中!!!马上更新!!!!!  01题意:有一个n个点的图,对于任意两个不同的点,他的权值是两个点下标的最小公倍数,求最小生出 ...

  8. cf786C(xjb)

    题目链接:http://codeforces.com/problemset/problem/768/C 题意:给出一个数组,经过k次操作后最大元素和最小元素分别是什么.. 操作:给当前数组排序,再将第 ...

  9. IT兄弟连 JavaWeb教程 JSP与Servlet的联系

    Servlet是使用Java Servlet接口(API)运行在Web服务器上的Java程序,其功能十分强大,它不但可以处理HTTP请求中的业务逻辑,而且还可以输出HTML代码来显示指定页面,而JSP ...

  10. idea svn 问题

    https://blog.csdn.net/liyantianmin/article/details/52837506