문제
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.
제한
Constraints:
- 1 <= nums.length <= 104
- -104 <= nums[i] <= 104
- nums contains distinct values sorted in ascending order.
- -104 <= target <= 104
예시
풀이
import bisect
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
idx = bisect.bisect_left(nums, target)
return idx
'Algorithm' 카테고리의 다른 글
[백준/Python] 피보나치 수 5 (0) | 2021.10.13 |
---|---|
[LeetCode/Python] Two Sum II - Input array is sorted (0) | 2021.10.11 |
[백준/Python] 숫자 카드 (0) | 2021.10.09 |
[백준/Python] 단지번호붙이기 (0) | 2021.10.09 |
[백준/Python] 데스 나이트 (0) | 2021.10.09 |