From 9073689339b441d3afe8d845d4a6b0763881196b Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Sun, 1 Jan 2023 19:47:54 -0500 Subject: [PATCH] Time: 70 ms (71.30%), Space: 14.6 MB (65.80%) - LeetHub --- ...ast-position-of-element-in-sorted-array.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 34-find-first-and-last-position-of-element-in-sorted-array/34-find-first-and-last-position-of-element-in-sorted-array.py diff --git a/34-find-first-and-last-position-of-element-in-sorted-array/34-find-first-and-last-position-of-element-in-sorted-array.py b/34-find-first-and-last-position-of-element-in-sorted-array/34-find-first-and-last-position-of-element-in-sorted-array.py new file mode 100644 index 0000000..3a26278 --- /dev/null +++ b/34-find-first-and-last-position-of-element-in-sorted-array/34-find-first-and-last-position-of-element-in-sorted-array.py @@ -0,0 +1,60 @@ +class Solution(object): + def searchRange(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + # Base cases + if len(nums) == 0: + return [-1,-1] + + if nums[0] > target: + return [-1,-1] + + if nums[-1] < target: + return [-1,-1] + + if len(nums) == 1: + return [0,0] if nums[0] == target else [-1,-1] + + foundIndex = -1 + + # Searchable region bounds + searchLow = 0 + searchHigh = len(nums) - 1 + + # Find an instance of target + while searchLow < searchHigh: + # Base case: length of bounds = 1 + if searchHigh - searchLow == 1: + if nums[searchLow] == target: + foundIndex = searchLow + elif nums[searchHigh] == target: + foundIndex = searchHigh + break + + # Standard search + midpointIndex = int((searchHigh - searchLow) / 2) + searchLow + if nums[midpointIndex] == target: + foundIndex = midpointIndex + break + elif nums[midpointIndex] < target: + searchLow = midpointIndex + else: + searchHigh = midpointIndex + + # Target not found + if nums[foundIndex] != target: + return [-1,-1] + + # Find all occurances of target + firstOcc = foundIndex + lastOcc = foundIndex + while firstOcc - 1 >= 0 and nums[firstOcc - 1] == target: + firstOcc -= 1 + + while lastOcc + 1 < len(nums) and nums[lastOcc + 1] == target: + lastOcc += 1 + + return [firstOcc, lastOcc] \ No newline at end of file