1D and 2D minmum

As I understand the problem, an entry is is a local minimum (LM) if it is smaller than all of its immediate neighbors.  (Note that at the edges there are less neighbors to consider than at interior entries.)  In the 2D case the neighbors are defined only in the horizontal and vertical directions, but this doesn't really make a difference, as we shall see.

The 1D case is solved by binary search:
If a[1] < a[2], a[1] is an LM.
Otherwise, if a[n-1] > a[n], a[n] is an LM.
Otherwise, the array entries start out descending (going from a[1] to a[2]) and ends up ascending (going from a[n-1] to a[n]).  It follows that an LM must exist somewhere in between.  Examine a[n/2].  If a[n/2] > a[n/2+1], then by the same reasoning there is an LM in the upper half of the array.  Similarly, if a[n/2] < a[n/2+1] there is an LM in the lower half of the array.  Solve recursively in the appropriate half.


 
posted @ 2013-09-11 01:18  pgu2  阅读(238)  评论(0)    收藏  举报