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 write an algorithm with O(log n) runtime complexity.
정렬된 배열에 target을 삽입하려 할 때 적절한 위치를 찾는 문제이다.
파이썬의 이진탐색 bisect 라이브러리에는 bisect_left, bisect_right등 다양한 메서드들을 제공한다.
이진탐색을 직접 구현해서 풀이할 수 있지만 이미 숙지하고 있으므로 라이브러리의 사용도 중요하다 생각해서 라이브러리를 사용했다.
from bisect import bisect_left
class Solution(object):
def searchInsert(self, nums, target):
return bisect_left(nums, target)