Simple Java Solution


  • 0
    C

    Algorithm : Binary Search

    1. check if mid element smaller than the next one, if true, start = mid+1

    2. check if mid element larger than the previous element, if true, end = mid

      public class Solution {
      public int findPeakElement(int[] nums) {
      int start = 0, end = nums.length-1;
      while(start < end) {
      int mid = start + (end - start)/2;
      if(mid < nums.length-1 && nums[mid] < nums[mid+1]) {
      start = mid+1;
      }else if (mid < nums.length-1 && nums[mid] > nums[mid+1]) {
      end = mid;
      }
      }
      return start;
      }
      }


Log in to reply
 

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