Search Insert Position
Easy
LC #35
binary searchlower boundNot attempted yet
Given a sorted array nums of distinct integers and a
target, return the index of target if it's
present. If not, return the index where it would have
to be inserted to keep nums sorted.
Aim for O(log n) time.
Example 1
Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2
Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3
Input: nums = [1,3,5,6], target = 7
Output: 4
Explanation: 7 goes after every value.
Constraints
- 1 <= nums.length <= 10^5
- -10^9 <= nums[i], target <= 10^9
- All values are distinct and sorted ascending.