Taken from mit 6.006 : To find a peak in a 2D array, where a number is a peak if it is >= than all its neighbours:
- Pick middle column j = m/2
- Find global maximum on column j at (i, j)
- Compare (i, j − 1),(i, j),(i, j + 1)
- Pick left columns of (i, j − 1) > (i, j), Similarly for right
- (i, j) is a 2D-peak if neither condition holds
- Solve the new problem with half the number of columns.
- When you have a single column, find global maximum and you‘re done.
I understand why it might find a peak, but I think it only finds a peak on half of the array if it exists
- Using binary search here confuses me since (1) the 2d array is not sorted, and each time you halve you are essentially saying there can be no peak on the left(which is not confirmed?)
- It finds the maximum element in the middle column - This ignores the possibility of a peak formed from non-maximal numbers, or that you can have more than one 1D peak in that column
- They compare numbers to the left and right of the max of the middle column - this discounts that there may be elements in the left and right column that are larger than max but not adjacent
Can someone explain to me why this algorithm is correct, hopefully by explaining (1)(2)(3)