Skip to content

Bug Report for find-target-in-rotated-sorted-array #4314

Open
@faqihisaninja

Description

@faqihisaninja

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:

Image

However, when submitted, the same test case fails due to a time limit exceeded error as shown below:

Image

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions