-
-
Notifications
You must be signed in to change notification settings - Fork 50.2k
Add Python solutions for Search Insert Position and Search Range #13422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from typing import List | ||
|
Check failure on line 1 in data_structures/Binary search algo/search_insert_position.py
|
||
|
|
||
|
|
||
| def search_insert(nums: List[int], target: int) -> int: | ||
| """ | ||
| Finds the index to insert a target value into a sorted list. | ||
|
|
||
| If the target is found, its index is returned. Otherwise, returns the | ||
| index where it would be if it were inserted in order. | ||
|
|
||
| Args: | ||
| nums: A sorted list of integers. | ||
| target: The integer to search for. | ||
|
|
||
| Returns: | ||
| The index for the target value. | ||
| """ | ||
| left, right = 0, len(nums) - 1 | ||
|
|
||
| while left <= right: | ||
| mid = (left + right) // 2 | ||
| if nums[mid] == target: | ||
| return mid | ||
| elif nums[mid] < target: | ||
| left = mid + 1 | ||
| else: | ||
| right = mid - 1 | ||
| return left | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from typing import List | ||
|
Check failure on line 1 in data_structures/Binary search algo/search_range.py
|
||
|
|
||
|
|
||
| def search_range(nums: List[int], target: int) -> List[int]: | ||
|
Check failure on line 4 in data_structures/Binary search algo/search_range.py
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
| """ | ||
| Finds the first and last position of a given target value in a sorted array. | ||
|
|
||
| Args: | ||
| nums: A sorted list of integers. | ||
| target: The integer to search for. | ||
|
|
||
| Returns: | ||
| A list containing the starting and ending index of the target. | ||
| Returns [-1, -1] if the target is not found. | ||
| """ | ||
|
|
||
| def find_bound(is_first: bool) -> int: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
| """Helper to find the leftmost or rightmost occurrence of the target.""" | ||
| left, right = 0, len(nums) - 1 | ||
| bound = -1 | ||
| while left <= right: | ||
| mid = (left + right) // 2 | ||
| if nums[mid] == target: | ||
| bound = mid | ||
| if is_first: | ||
| right = mid - 1 # Continue searching on the left | ||
| else: | ||
| left = mid + 1 # Continue searching on the right | ||
| elif nums[mid] < target: | ||
| left = mid + 1 | ||
| else: | ||
| right = mid - 1 | ||
| return bound | ||
|
|
||
| first_bound = find_bound(True) | ||
| if first_bound == -1: | ||
| return [-1, -1] | ||
|
|
||
| last_bound = find_bound(False) | ||
| return [first_bound, last_bound] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
data_structures/Binary search algo/search_insert_position.py, please provide doctest for the functionsearch_insert