One-dimensional Peak finding problem
Definition
A peak in an array is a position that is greater than or equal to its adjacent elements. If the peak is an edge element — the first or the last element in the array — then it must be greater than or equal to its left-adjacent or right-adjacent element, respectively.
Problem: Find the peak in an array
So does a peak always exist?
Yes, it does, and there is a simple test for it. Start by thinking of all the different cases: when the elements are in increasing order, decreasing order, are all equal, and in random order.
Why ≧ and not simply >?
Think about the case when all elements are the same — a peak will never exist.
Brute-force algorithm
Start from the left and compare adjacent elements to each other until a peak is found.
n/2 elements on average
Worst-case time complexity: Θ(n)
Divide and conquer
There is a simple, yet efficient, approach to this problem: start looking from the middle.
For an array A[] of n elements,
- if A[n/2 - 1] ≧ A[n/2], then look for the peak in the left half
- if A[n/2 + 1] ≧ A[n/2], then look for the peak in the right half
- Else, A[n/2] is the peak (trivial)
1 | from typing import List |
Summary
That was a simple one-dimensional, peak-finding problem. I will look at the two-dimensional variant in the next post :)
Edit: Here is the second post