Open
Description
Bug Report for https://neetcode.io/problems/find-target-in-rotated-sorted-array
The test case:
nums = [3, 4, 5, 6, 1, 2]
target = 1
was passed when running against the first 2 algorithms as shown below:
However, when submitted, the same test case fails due to a time limit exceeded error as shown below:
This is my code to reproduce the bug:
class Solution:
def search(self, nums: List[int], target: int) -> int:
l, r = 0, len(nums) - 1
m = (r + l) // 2
while l < r:
if nums[m] == target:
return m
if nums[l] < nums[m]:
# Left side is sorted
if target >= nums[l] and target < nums[m]:
# In the left sorted array
r = m - 1
else:
# In the right array
l = m + 1
elif nums[m] < nums[r]:
# Right side is sorted
if target > nums[m] and target <= nums[r]:
# In the right sorted array
l = m + 1
else:
# In the left array
r = m - 1
m = (r + l) // 2
return m if nums[m] == target else -1
Metadata
Metadata
Assignees
Labels
No labels