Most efficient way to search a sorted matrix?

后端 未结 9 1543
野的像风
野的像风 2020-12-29 14:02

I have an assignment to write an algorithm (not in any particular language, just pseudo-code) that receives a matrix [size: M x N] that is sorted in a way that all of it\'s

9条回答
  •  青春惊慌失措
    2020-12-29 14:33

    in log M you can get a range of rows able to contain the target (binary search on the first value of rows, binary search on last value of rows, keep only those rows whose first <= target and last >= target) two binary searches is still O(log M)
    then in O(log N) you can explore each of these rows, with again, a binary search!

    that makes it O(logM x logN)
    tadaaaa

提交回复
热议问题