Leetcode 378. Kth Smallest Element in a Sorted Matrix
来源:互联网 发布:西贝柳斯 Mac 软件下载 编辑:程序博客网 时间:2024/06/05 17:41
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.
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.
public int kthSmallest(int[][] matrix, int k) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return -1; PriorityQueue<Integer> pq = new PriorityQueue<Integer>(k, (x,y) -> y - x); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { pq.offer(matrix[i][j]); if (pq.size() > k) pq.poll(); } } return pq.peek(); }
阅读全文
0 0
- LeetCode—378. Kth Smallest Element in a Sorted Matrix
- Leetcode 378. Kth Smallest Element in a Sorted Matrix
- [leetcode] 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
- Leetcode-378. Kth Smallest Element in a Sorted Matrix
- 378. [LeetCode]Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- 【LeetCode】 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- [LeetCode]378. Kth Smallest Element in a Sorted Matrix
- Leetcode 378. Kth Smallest Element in a Sorted Matrix
- LeetCode笔记:378. Kth Smallest Element in a Sorted Matrix
- LeetCode: 378. Kth Smallest Element in a Sorted Matrix
- leetcode 378. Kth Smallest Element in a Sorted Matrix
- Android应用程序访问硬件驱动(JNI方式)
- Java面试题
- angular4 引入bootstrap
- 1011. World Cup Betting (20)
- JSP中页面传值方法
- Leetcode 378. Kth Smallest Element in a Sorted Matrix
- 78. Subsets 解法二
- 视觉定位系统在机器人全场定位的应用
- windows sublime 安装 opencv3.0 + anaconda
- Swift中Delegate的正确使用姿势
- staitic(静态)关键字
- 光晕感的文字,带下划线的特效等 css 代码
- mysql5.7在Linux的安装
- 合格的PHP程序员必备技能