思路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. BZOJ_3123_[Sdoi2013]森林_主席树+启发式合并

    BZOJ_3123_[Sdoi2013]森林_主席树+启发式合并 Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20 ...

  2. PHP多种序列化/反序列化的方法 json_encode json_decode

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...

  3. MongoDB分析工具之三:db.currentOp()

    db.currentOp() db.currentOp是个好东西,顾名思义,就是当前的操作.在mongodb中可以查看当前数据库上此刻的操作语句信息,包括insert/query/update/rem ...

  4. TQ210 —— S5PV210 uboot顶层Makefile分析

    转自:http://blog.csdn.net/wqx521/article/details/52469759 # (C) Copyright 2000-2008 # Wolfgang Denk, D ...

  5. Flutter实战视频-移动电商-63.购物车_详细页显示购物车商品数量

    63.购物车_详细页显示购物车商品数量 购物车的图标嵌套在statck组件里面 外层套了一个stack组件 数量我们需要用Provide 返回一个container来做样式 气泡效果,中间是个数字外面 ...

  6. POJ - 2251 Dungeon Master 多维多方向BFS

    Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is ...

  7. HDU - 2036 改革春风吹满地 叉乘法求多边形面积

    改革春风吹满地 “ 改革春风吹满地, 不会AC没关系; 实在不行回老家, 还有一亩三分地. 谢谢!(乐队奏乐)” 话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里雾里,而且,还竟 ...

  8. HDU4801【DFS】

    参考:大牛博客 题意:  给你一个2阶魔方,给你24个数代表颜色,然后让你求在<=n次操作里面最多能搞出几面是一样的. 思路: 就是一个DFS嘛,就是怎么转搞出来: 上面: 22 23 4 0 ...

  9. tp5 验证码功能实现

    视图层 <div class="loginbox-textbox"> <input class="form-control" placehol ...

  10. Mol Cell Proteomics. |王欣然| 基于微粒的蛋白聚合物捕获技术让能满足多种不同需求的蛋白质组学样品制备方法成为可能

    大家好,本周分享的是发表在Molecular & Cellular Proteomics. 上的一篇关于蛋白质组学样本质谱分析前处理方法改进的文章,题目是Protein aggregation ...