Problem
Given a sorted array 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 may assume no duplicates in the array.
Examples
1 | Input: [1,3,5,6], 5 |
1 | Input: [1,3,5,6], 2 |
1 | Input: [1,3,5,6], 7 |
1 | Input: [1,3,5,6], 0 |
Solution1
Method: Brute Force
Time Complexity: O(n)
Space Complexity: O(1)
1 | class Solution: |
Solution2
Method: Binary Search
Time Complexity: O (log n)
Space Complexity: O(1)
1 | class Solution: |
or
1 | class Solution: |