Skip to content

Latest commit

 

History

History

35-SearchInsertPosition

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Search Insert Position

Problem can be found in here!

Solution: Binary Search

def searchInsert(nums: List[int], target: int) -> int:
    left, right = 0, len(nums)

    while left < right:
        mid = (left + right) // 2
        if nums[mid] == target:
            return mid
        elif nums[mid] <= target:
            left = mid + 1
        else:
            right = mid

    return left

Time Complexity: O(logn), Space Complexity: O(1)