Find local minima in an array

后端 未结 7 1583
时光说笑
时光说笑 2020-12-04 09:56

Given an array of integers, find the local minima. An element A[i] is defined as a local minimum if A[i-1] > A[i] and A[i] < A[i+1] where i = 1...n-2. In case of boundar

相关标签:
7条回答
  • 2020-12-04 10:32

    You algorithm will not work for this array

    15, 13, 12, 18, 19, 20, 7, 6, 5, 4, 3, 2, 1
    

    Here the local minima is 12.. but when I check teh middle element, which is 7, you algorithm will discard the left half (which has the minima) and check in the right half. Hence it does not work

    I think it will only work for the special case when the array has a special property that A[1] ≥ A[2] and A[n − 1] ≤ A[n].

    0 讨论(0)
  • 2020-12-04 10:32

    Here is a solution that works on O(log n). Basically, this works on the merge sort approach (divide and conquer).

    public class LocalMaxima {
        int []a = {5,8,10,25,6,3,44,51,55,56,57,58,34,5,59};
    
        @Test 
        public  void localMaxima () {
            System.out.println((a[localMaxima(0,a.length-1)]));
        }
    
        int localMaxima(int low, int high) {
            if(high-low > 2) {
                int mid = (low+high)/2;
                return maxof(localMaxima(low,mid),localMaxima(mid+1, high));
            }
            else if(high-low == 1) {
                return maxof(high,low);
            }
            else if(high-low == 0) {
                return high;
            }
            if(high-low == 2) {
                return maxof(maxof(low, high),low+1);
            }
            return 0;
        }
    
        int maxof(int i, int j) {
            if(a[i] <a[j]) {
                return j;
            }
            else {
                return i;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 10:40

    If the array elements are not guaranteed to be distinct, then it's not possible to do this in O(log n) time. The reason for this is the following: suppose that you have an array where all n > 1 values are the same. In this case, none of the elements can be local minima, because no element is less than its neighbors. However, in order to determine that all values are the same, you will have to look at all the array elements, which takes O(n) time. If you use less than O(n) time, you can't necessarily look at all the array elements.

    If, on the other hand, the array elements are guaranteed to be distinct, you can solve this in O(log n) time using the following observations:

    1. If there is just one element, it's guaranteed to be a local minimum.
    2. If there are multiple elements, look at the middle element. If it's a local minimum, you're done. Otherwise, at least one of the elements next to it must be smaller than it. Now, imagine what would happen if you were to start at one of the smaller elements and progressively move toward one of the ends of the array in the direction away from the middle element. At each step, either the next element is smaller than the previous, or it will be bigger. Eventually, you will either hit the end of the array this way, or you will hit a local minimum. Note that this means that you could do this to find a local minimum. However, we're not actually going to do that. Instead, we'll use the fact that a local minimum will exist in this half of the array as a justification for throwing away one half of the array. In what remains, we are guaranteed to find a local minimum.

    Consequently, you can build up the following recursive algorithm:

    1. If there is just one array element, it's a local minimum.
    2. If there are two array elements, check each. One must be a local minimum.
    3. Otherwise, look at the middle element of the array. If it's a local minimum, return it. Otherwise, at least one adjacent value must be smaller than this one. Recurse in the half of the array containing that smaller element (but not the middle).

    Notice that this has the recurrence relation

    T(1) ≤ 1

    T(2) ≤ 1

    T(n) ≤ T(n / 2) + 1

    Using the Master Theorem, you can show that this algorithm runs in time O(log n), as required.

    Hope this helps!

    Please also notice that this algorithm only works if edges of the array count as local minima if they are smaller than the adjacent element.

    0 讨论(0)
  • 2020-12-04 10:42

    The number of local minima can be n/2; you can't enumerate them all in O(log n) time.

    0 讨论(0)
  • 2020-12-04 10:42

    The original question is not complete.

    Just found the complete question and full-detailed explanation at Find local minima in an array! - not my blog

    Given an array of unique integers whose first two numbers are decreasing and last two numbers are increasing, find a number in the array which is local minima. A number in the array is called local minima if it is smaller than both its left and right numbers.

    For example in the array 9,7,2,8,5,6,3,4 2 is a local minima as it is smaller than its left and right number 7 and 8. Similarly 5 is another local minima as it is between 8 and 6, both larger than 5.

    You need to find any one of the local minima.

    0 讨论(0)
  • 2020-12-04 10:47

    Use a divide-and-conquer algorithm. Let m = n/2, and examine the value A[m] (that is, the element in the middle of the array).

    Case 1: A[m−1] < A[m]. Then the left half of the array must contain a local minimum, so recurse on the left half. We can show this by contradiction: assume that A[i] is not a local minimum for each 0 ≤ i < m. Then A[m−1] is not a local minimum, which implies that A[m−2] < A[m−1]. Similarly, A[m −3] < A[m −2]. Continuing in this fashion, we obtain A[0] < A[1]. But then A[0] is a local minimum, contrary to our initial assumption.

    Case 2: A[m + 1] > A[m]. Then the right half of the array must contain a local minimum, so recurse on the right half. This is symmetrical to Case 1.

    Case 3: A[m − 1] > A[m] and A[m + 1] < A[m]. Then A[m] is a local minimum, so return it. The running time recurrence is T(n) = T(n/2) + Θ(1), which yields T(n) = Θ(log n).

    0 讨论(0)
提交回复
热议问题