https://leetcode.com/problems/search-insert-position/description/
Search Insert Position - LeetCode
Can you solve this real interview question? Search Insert Position - Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must w
leetcode.com
삽입 정렬 구현 문제.
log(n)으로 풀어야 한다.
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
L_idx = 0
R_idx = len(nums) - 1
while L_idx <= R_idx:
middle = (L_idx+R_idx) //2
if target == nums[middle]:
return middle
elif target > nums[middle]:
L_idx = middle + 1
elif target < nums[middle]:
R_idx = middle - 1
return L_idx
'알고리즘' 카테고리의 다른 글
파이썬 코딩테스트 벼락치기(porting from 자바스크립트) (2) | 2025.04.03 |
---|---|
leetcode 88 - Merge sorted Array (2) | 2023.05.28 |
백준 2839 - 설탕배달 (python) (1) | 2023.05.21 |
백준 2178 - 미로탐색 (python) (0) | 2023.05.21 |
백준 10814 - 나이순 정렬 (class2) (python) (0) | 2023.05.14 |