Divide and conquer algorithm applied in finding a peak in an array.

自古美人都是妖i 提交于 2019-11-28 13:45:27
SomeWittyUsername

The algorithm is correct, although it requires a bit of calculus to prove. First case is trivial → peak. Second case is a "half peak", meaning that it has the down slope, but not up.

We have 2 possibilities here:

  • The function is monotonically decreasing till amid → a1 ≥ a2 is the peak.
  • The function is not monotonically decreasing till amid → There is a 1≥k≥mid, for which ak ≥ ak-1. Let's choose the largest k for which this statement is true → ak+1 < ak ≥ ak-1 → and that's the peak.

Similar argument can be applied for the third case with the opposite "half peak".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!