且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

算法:寻找峰一行

更新时间:2023-02-26 16:42:07

这是 O(log n)的算法存在。我们采用分而治之。

An O(log n) algorithm exists. We use divide-and-conquer.

find_peak(lo,hi):
  mid = (lo+hi)/2
  if A[mid] >= A[mid-1], A[mid+1] return mid
  if A[mid] < A[mid-1] 
    return find_peak(lo,mid-1) // a peak must exists in A[1..mid-1]
  if A[mid] < A[mid+1]
    return find_peak(mid+1,hi) // a peak must exists in A[mid+1..hi]