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.
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: 4Constraints:
1 <= nums.length <= 104-104 <= nums[i] <= 104numscontains distinct values sorted in ascending order.-104 <= target <= 104
The logic of the code is as follows:
First, it checks if the target value is already present in the given
numslist using theif target in numscondition.If the target is present, it returns the index of the target using the
nums.index(target)method. This method returns the index of the first occurrence of the target value in the list.If the target is not present in the list, it enters the
elseblock and initializes the variableito 0.It then enters a
forloop that iterates over each element in thenumslist. The loop variable isi.Inside the loop, it checks if the target value is less than the current element
nums[i]using the conditionif target < nums[i].If the target value is less than
nums[i], it means that the target should be inserted at the current indexi. In this case, it returnsias the index where the target should be inserted.If the target value is greater than or equal to
nums[i], it continues to the next iteration of the loop.After the loop completes, it returns
i+1as the index where the target should be inserted. This handles the case where the target value is greater than all the elements in the list, and it should be inserted at the end.
nums list or the target. It only uses a few variables (n, l, r, mid, and target) that occupy a constant amount of space regardless of the input size.If the
numslist is empty, it means there are no elements to search. In this case, the function returns 0.If the
numslist is not empty, the function initializes the variablesn,l, andr.nrepresents the length of thenumslist,lrepresents the lower bound index, andrrepresents the upper bound index.The code enters a while loop with the condition
l < r. This loop continues until the lower boundlbecomes greater than or equal to the upper boundr.Inside the loop, the code calculates the middle index
midusing the formulamid = l + (r - l) // 2. This finds the midpoint between the lower and upper bounds, ensuring integer division is used.The code then compares the value at the middle index
nums[mid]with the target value.If the value at
nums[mid]is equal to the target, it means the target is found at indexmid. In this case, the function returnsmid.If the value at
nums[mid]is greater than the target, it means the target should be present in the left half of the search range. The upper boundris updated tomid, effectively narrowing down the search space to the left half.If the value at
nums[mid]is less than the target, it means the target should be present in the right half of the search range. The lower boundlis updated tomid + 1, effectively narrowing down the search space to the right half.After the while loop finishes, it means the target is not present in the
numslist. The code checks if the target is greater than the value at indexlto determine the correct index to insert the target.If the target is greater than the value at index
l, it means the target should be inserted after the value at indexl. In this case, the function returnsl + 1.If the target is not greater than the value at index
l, it means the target should be inserted at or before the value at indexl. In this case, the function returnsl.
Comments
Post a Comment