O(logN) Solution and Condensed Code


  • 0
    G
    public int findPeakElement(int[] nums)
    {
         if(nums.length == 1 || (nums[0] > nums[1])) return 0;
     
         if(nums[nums.length-1] > nums[nums.length-2]) return (nums.length -1);
         
         return findPeak(nums, 0, nums.length);
    }
     
    int findPeak(int[] nums, int start, int end)
    {
         int length = end - start;
         
         if(nums[start + length/2] < nums[start + (length/2) - 1])
             return findPeak(nums, start, start + length/2);
    
         else if(nums[start + length/2] < nums[start + (length/2) + 1])
             return findPeak(nums, start + length/2, end);
    
         return start + (length/2);
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.