sorted matrix - search & find-k-th

sorted matrix ( Young Matrix )

search for a given value in the matrix:

1) starting from upper-right corner, turn left or turn down, O(n+m)

2) if it‘s square, O(n+m) is optimal, see http://stackoverflow.com/a/10597806

3) if it‘s not square, e.g. in extreme case it degenerates to a sorted array, the complexity is lower-bounded to O(m*(1+log(n/m)) (suppose m < n)

4) page http://twistedoakstudios.com/blog/Post5365_searching-a-sorted-matrix-faster has more details

find k-th element in the matrix: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/

a) heap-based, starting from top-left corner element, push to heap; each time an element is poped, push its right and down elements.

  complexity is k*log(k)

b) binary-search on target value, starting from [smallest, largest], and then count how many elements are less than this value to see if it‘s kth element. O(n*log(n)*log(N)) where n is number of rows, N is (largest-smallest)

时间: 2024-10-26 10:12:11

sorted matrix - search & find-k-th的相关文章

Monotone and Sorted Matrix Search

monotone_matrix_search() and sorted_matrix_search() are techniques that deal with the problem of efficiently finding largest entries in matrices with certain structural properties. Many concrete problems can be modelled as matrix search problems, and

Kth Smallest Number in Sorted Matrix

Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5. 分析: Although the matrix is horizontally and vertically sorted, there is no rule to find the next smalles

[Lintcode] Kth Smallest Number in Sorted Matrix

Kth Smallest Number in Sorted Matrix Find the kth smallest number in at row and column sorted matrix. Have you met this question in a real interview? Yes Example Given k = 4 and a matrix: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5 Challenge O(k lo

Kth Smallest Element in Sorted Matrix

1 public class Solution { 2 public int kthSmallest(int[][] matrix, int k) { 3 if (matrix.length == 0 || matrix[0].length == 0 || k <= 0) { 4 return -1; 5 } 6 PriorityQueue<PayLoad> queue = new PriorityQueue<>(); 7 for (int i = 0; i < mat

【LeetCode-面试算法经典-Java实现】【023-Merge k Sorted Lists(合并k个排好的的单链表)】

[023-Merge k Sorted Lists(合并k个排好的的单链表)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题目大意 合并k个排好的的单链表.分析和描述它的复杂性. 解题思路 使用小顶堆来实现,先将K个链表的头结点入堆,取堆顶元素,这个结点就是最小的,接

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

Search In Sorted Matrix I

Given a 2D matrix that contains integers only, which each row is sorted in an ascending order. The first element of next row is larger than (or equal to) the last element of previous row. Given a target number, returning the position that the target

378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [   [ 1,  5,  9],   [10, 11, 13],   [12, 13, 15]],k = 8,返回 13.说明:你可以假设 k 的值永远是有效的, 1 ≤ k ≤ n2 .详见:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/des

378. Kth Smallest Element in a Sorted Matrix

https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/#/solutions http://www.cnblogs.com/EdwardLiu/p/6109080.html Heap : public class Solution { public int kthSmallest(int[][] matrix, int k) { int n = matrix.length; PriorityQueue<Tupl