Frame
Binary Search
배열이나 답 범위를 절반씩 좁혀가며 탐색하는 기본 이진 탐색 패턴과 최대·최소 해를 찾는 응용 전략
Oct 16, 2025 — binary-search
- 매번 search space 를 절반으로 줄여나가면서 찾면서
O(logn)
시간으로 target 을 찾는 방법 - 배열에서 조건을 만족하는 특정한 값을 찾는 문제
const fn = (arr, target) => { let left = 0; let right = arr.length - 1;
while (left <= right) { let mid = Math.floor((left + right) / 2); if (arr[mid] == target) { // do something return; } if (arr[mid] > target) { right = mid - 1; } else { left = mid + 1; } }
// left is the insertion point return left;}
- LeetCode’s Interview Crash Course: Data Structures and Algorithms (opens in a new window)
- 코딩 인터뷰를 위한 알고리즘 치트시트 (opens in a new window)