Find First and Last Position of Element in Sorted Array

Given an array of integers sorted in non-decreasing order, find the starting and ending position of a given target value. If the target is not found, return [-1, -1]. The algorithm must have O(log n) runtime complexity.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • nums is a non-decreasing array.
  • -10^9 <= target <= 10^9
Saving… · 2 lines
class Solution:
def findFirstAndLastPosition(self, nums: List[int], target: int) -> List[int]:
Console

Click Run to execute sample tests.