easy +10 pts

Find Indices of Target in Sorted List

Search a sorted list for the first and last occurrence of a target using binary search.

Write a function `search_range(nums, target)` that takes a **sorted** list of integers `nums` and an integer `target`, and returns a list `[first, last]` where: - `first` is the index of the first occurrence of `target` in `nums`, - `last` is the index of the last occurrence of `target` in `nums`. If `target` is not present, return `[-1, -1]`. The indices are **0-based**. The list may be empty. Your solution must run in **O(log n)** time (use binary search).

Constraints

Constraints: - `0 <= len(nums) <= 10^5` - `-10^9 <= nums[i] <= 10^9` - The input list is sorted in non-decreasing order, and each query is a single call. - Complexity: O(log n) time, O(1) extra space.

Example

```python
>>> search_range([5, 7, 7, 8, 8, 10], 8)
[3, 4]
>>> search_range([5, 7, 7, 8, 8, 10], 6)
[-1, -1]
>>> search_range([], 0)
[-1, -1]
```
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use two separate binary searches: one to find the leftmost index where `target` could be placed, and one for the rightmost.
When searching for the first occurrence, if `nums[mid] >= target`, move `right` to `mid`; otherwise move `left` to `mid + 1`.
When searching for the last occurrence, if `nums[mid] <= target`, move `left` to `mid`; otherwise move `right` to `mid - 1`. Watch out for infinite loops—adjust the mid calculation accordingly.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.