KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • 백준
  • programmers
  • recursion
  • string
  • Level 0
  • Level1
  • js
  • 다이나믹 프로그래밍
  • C++
  • LeetCode
  • Level 1
  • 수학
  • 제코베 JS 100제
  • codeup
  • 문자열
  • 그래프
  • C
  • tree
  • 정렬
  • Level 2

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Binary Search / 33번 / Search in Rotated Sorted Array / JS

2023. 4. 28. 17:43

< 문제 바로가기 >

 

Search in Rotated Sorted Array - LeetCode

Can you solve this real interview question? Search in Rotated Sorted Array - There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <=

leetcode.com

 

< 문제 간단설명 >

정렬된 배열이 원형큐처럼 몇번 회전이 되었을지 알 수 없는 배열에서 target 값을 찾는 문제이다.

 

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function(nums, target) {
    let [left, right] = [0, nums.length - 1];

    while(left <= right) {
        let mid = Math.floor((left + right) / 2);

        if(nums[mid] === target) {
            return mid;
        }

        // 만약 left의 값이 mid 이하라면,
        // 즉 오름차순으로 잘 정렬이 되어있을 경우라면,
        if(nums[left] <= nums[mid]) {
            if(nums[left] <= target && target <= nums[mid]) {
                right = mid - 1;
            }
            else {
                left = mid + 1;
            }
        }

        // 만약 left의 값이 mid보다 크다면,
        // 즉 회전으로 인해 오름차순이 되어있지 않은 구간이라면,
        else if(nums[left] > nums[mid]) {
            if(nums[mid] <= target && target <= nums[right]) {
                left = mid + 1;
            }
            else {
                right = mid - 1;
            }
        }
    }

    return -1;
};
저작자표시

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Binary Search Tree / 230번 / Kth Smallest Element in a BST / JS  (0) 2023.04.29
LeetCode / Binary Search Tree / 108번 / Convert Sorted Array to Binary Search Tree / JS  (0) 2023.04.29
LeetCode / Binary Search / 74번 / Search a 2D Matrix / JS  (0) 2023.04.28
LeetCode / Tree / 437번 / Path Sum III / JS  (0) 2023.04.28
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Binary Search Tree / 230번 / Kth Smallest Element in a BST / JS
    • LeetCode / Binary Search Tree / 108번 / Convert Sorted Array to Binary Search Tree / JS
    • LeetCode / Binary Search / 74번 / Search a 2D Matrix / JS
    • LeetCode / Tree / 437번 / Path Sum III / JS
    KimMinJun
    KimMinJun

    티스토리툴바