码迷,mamicode.com
首页 > 其他好文 > 详细

sorted matrix - search & find-k-th

时间:2016-11-09 11:15:30      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:heap   man   studio   rem   targe   generate   ast   binary   details   

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)

 

sorted matrix - search & find-k-th

标签:heap   man   studio   rem   targe   generate   ast   binary   details   

原文地址:http://www.cnblogs.com/qsort/p/6045245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!