Algorithm | Find Peak Element, Search in Rotated Sorted Array

Updated:
1 minute read

본 글은 leetCode의 Find Peak Element / Search in Rotated Sorted Array 문제 솔루션을 JavaScript 코드로 정리한 글입니다.

Find Peak Element

문제 자세히 보기

Approach 1: Linear Scan

var findPeakElement = function(nums) {
  for(let i = 0; i < nums.length - 1; ++i) {
    if(nums[i] > nums[i + 1]) return i;
  }
  return nums.length - 1;
};
var findPeakElement = function(nums) {
  let lo = 0;
  let hi = nums.length - 1;
  let mid;
  while(lo < hi) {
    mid = Math.floor((lo + hi) / 2);
    if(nums[mid] < nums[mid + 1]) lo = mid + 1;
    else hi = mid;
  }
  return lo;
};

Search in Rotated Sorted Array

문제 자세히 보기

var search = function(nums, target) {
  let lo = 0;
  let hi = nums.length - 1;
  while(lo <= hi) {
    let mid = Math.floor((lo + hi) / 2);
    if(nums[mid] === target) return mid;
    if(nums[mid] > target) {
      if(target < nums[lo] && nums[mid] >= nums[lo]) lo = mid + 1;
      else hi = mid - 1;
    } else  {
      if(target > nums[hi] && nums[mid] <= nums[hi]) hi = mid - 1;
      else lo = mid + 1;
    }
  }
  return -1;
};
Back to top ↑

Leave a comment