Advertisement
Guest User

162. Find Peak Element | Time: O(log(n)) | Space: O(1)

a guest
Aug 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.51 KB | None | 0 0
  1. // Problem: https://leetcode.com/problems/find-peak-element
  2. // Solution: https://www.youtube.com/watch?v=CFgUQUL7j_c
  3.  
  4. class Solution {
  5.     public int findPeakElement(int[] nums) {
  6.         int left = 0;
  7.         int right = nums.length - 1;
  8.         while(left < right) {
  9.             int mid = left + (right - left) / 2;
  10.             if(nums[mid] < nums[mid + 1]) {
  11.                 left = mid + 1;
  12.             } else {
  13.                 right = mid;
  14.             }
  15.         }
  16.        
  17.         return left;
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement