Find local minima in an array

后端 未结 7 1584
时光说笑
时光说笑 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:48

    Actually my previous algorithm can be modified to get all the maxima in O(log n) time. I tested that it works great for all the input provided. Please let me know your feedback

    public class LocalMaximas {
    
    @Test
    public void test () {
        System.out.println("maximas: please modify code to handle if array size is <= 2");
    
        int []a = {5,8,10,25,6,3,44,51,55,56,57,58,34,5,59,2};
        localMaximas(a);
    
        int []b = {9,7,2,8,5,6,3,4, 2}; //9,8,6,4
        localMaximas(b);
    
        int [] c= {15, 13, 12, 18, 19, 20, 7, 6, 5, 4, 3, 2, 1};//15,20
        localMaximas(c);
    }
    
    public  void localMaximas (int [] a) {
        System.out.println("\n\n");
        if(isMaxima(a,0)) {
            System.out.println(a[0]);
        }
        if(isMaxima(a,a.length-1)) {
            System.out.println(a[a.length-1]);
        }
        localMaximas(a,0,a.length-1);
    }
    
    int localMaximas(int []a,int low, int high) {
        int mid = (low+high)/2;
        if(high-low > 3) {     // more than 4 items in currently  divided array
            if(isMaxima(a,mid)) {
                System.out.println(a[mid]);
            }   
            localMaximas(a,low, mid);
            localMaximas(a,mid, high);
        }
        else if(high-low == 3){ //exactly 4 items in currently  divided array
            localMaximas(a,low, mid+1);
            localMaximas(a,mid, high);
        }   
        else if((high-low == 2) && (isMaxima(a,low+1))) {
            System.out.println(a[low+1]);
        }
        return 0;
    }
    
    int maxof(int []a, int i, int j) {
        if(a[i] <a[j]) {
            return j;
        }
        else {
            return i;
        }
    }
    
    boolean isMaxima(int []a ,int mid) {
        if(mid == 0) {
            if(maxof(a, mid, mid+1) == mid) {
                return true;
            }
            else {
                return false;
            }
        }
        else if(mid==a.length-1) {
            if(maxof(a,mid,mid-1) == mid) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            if((maxof(a, mid, mid+1) == mid) && (maxof(a, mid, mid-1) == mid)) {
                return true;
            }
            else {
                return false;
            }           
        }
    }
    }
    
    0 讨论(0)
提交回复
热议问题