【发布时间】:2017-03-05 01:22:47
【问题描述】:
找到p的伪代码算法是:
peakreturn(H)
for p=1 to m //m is the length of H
if H[p-1] ≤ H[p] and H[p] ≥ H[p+1] then return p
return nil // only returns this if there is no peak
假设我有一个包含 int 值的数组 H[1 到 m],其中“p”是峰值元素,如果:
H[p] ≥ H[p+1] if p = 1,
H[p-1] ≤ A[p] ≥ H[p+1] if 1 < p < m,
H[p] ≥ H[p-1] if p = m.
基本上,如果 H[p] 大于或等于其相邻元素,则 H[p] 就是峰值。
假设数组H在开头和结尾都大了1个元素,数组为H[0到m+1],其中H[0] = H[m+1] = -infinity。因此,H[0] 和 H[m+1] 是哨兵。那么元素 p,其中 1 ≤ p ≤ n,如果 H[p-1] ≤ H[p] ≥ H[p+1],则为一个峰值。
我认为渐近时间复杂度是 O(log n) 但我不确定。如果可以的话请帮忙,非常感谢。
【问题讨论】:
标签: algorithm big-o asymptotic-complexity