Skip to contentKEEP THE CRAFTTHE SANCTUARY

First and Last Target Position

Find where a target's run of copies starts and ends in a sorted array.

The problem

Implement Solution.searchRange(nums, target). nums is a list of up to 100,000 integers sorted in nondecreasing order, so equal values sit next to each other. Return [first, last], the first and last index where target appears, or [-1, -1] if target is not in nums. Your solution must run in O(log n) time.

Starter code

class Solution:
    def searchRange(self, nums, target):
        pass

Public examples

  1. nums = [5, 7, 7, 8, 8, 10], target = 8gives[3, 4]
  2. nums = [], target = 0gives[-1, -1]
  3. nums = [9], target = 9gives[0, 0]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.