How to determine if a sequence is bitonic?

后端 未结 5 1122
遥遥无期
遥遥无期 2020-12-23 18:00

A sequence is bitonic if it monotonically increases and then monotonically de- creases, or if it can be circularly shifted to monotonically increase and

5条回答
  •  死守一世寂寞
    2020-12-23 18:02

    You can look for the peak, i.e. when a[i-1] < a[i] && a[i] > a[i+1], then a[i] is the local peak (taking care of wrap around with modulus operator).

    In a bitonic sequence, there can only be one such peak. Once you found the peak, then you can walk downhill to the left (wrapping around as necessary, again using modulus) until you found an uphill. Then you go back to the peak, walk downhill to the right, until you found another uphill. Now, if a sequence is bitonic, then you will have covered the whole sequence by going downhill on both sides.

    btw, do I misunderstood the question, or is your first example not bitonic?

提交回复
热议问题