leetcode378 Kth Smallest Element in a Sorted Matrix
思路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的更多相关文章
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- [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 ...
- [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 ...
- 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 ...
- 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: 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
Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...
- 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 ...
随机推荐
- Junit单元测试类
/*package zxdc.web; import static org.junit.Assert.*; import java.io.IOException; import javax.servl ...
- C# 获取QQ好友列表信息的实现
分析部分 当我们访问QQ空间的时候,大家可以在右侧的发现一个这样的统计信息 当点击这个链接的时候,会跳转到 这样一个URL 这个URl可以管理好友,当然也就能读取到好友 上面我们是在浏览器中的操 ...
- bzoj 3481 DZY Loves Math III——反演+rho分解质因数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3481 推推式子发现:令Q=gcd(P,Q),ans=Σ(d|Q) d*phi(P/d).把 ...
- bzoj4589
fwt 原理并不知道 nim游戏石子异或和=0后手赢 那么也就是求a[1]^a[2]^...^a[n]=0的方案数 这个和bzoj3992一样可以dp dp[i][j]表示前i个数异或和为j的方案数 ...
- AngularJS系统学习之Module(模块)
本文源自:http://blog.csdn.net/woxueliuyun/article/details/50962645 学习之后略有所得, 来此分享.建议看原文. 模块是提供一些特殊服务的功能块 ...
- Identity Server 4 原理和实战(完结)_Resource Owner Password Credentials 授权实例
今天要讲的 用fiddler来监听,昨天的客户端的请求 这是一个post的请求 这是响应的数据 Expores_in超时时间, 今天的内容 在服务端再声明一个client端 wpf的应用的效果图 首先 ...
- 原创|高逼格企业级MySQL数据库备份方案,原来是这样....
很多人,这里说的是运维工程师们,一提到写某某方案,很是头疼.不是上某度一统搜索,就是同样一句话在N个群全部群发一遍:“有没有某某方案,可以共享一下的吗??求助,各位大佬们”,估计十有八九,全部石沉大海 ...
- 洛谷 - P2257 - YY的GCD - 莫比乌斯反演 - 整除分块
https://www.luogu.org/problemnew/show/P2257 求 \(n,m\) 中 \(gcd(i,j)==p\) 的数对的个数 求 $\sum\limits_p \sum ...
- Unity中HideInInspector和SerializeField
http://blog.sina.com.cn/s/blog_697b1b8c0102uxvn.html Unity会自动为Public变量做序列化,序列化的意思是说再次读取Unity时序列化的变量是 ...
- [Xcode 实际操作]二、视图与手势-(10)UITapGestureRecognizer手势之单击
目录:[Swift]Xcode实际操作 本文将演示使用视图的手势功能,完成视图的交互操作. import UIKit class ViewController: UIViewController { ...