Skip to content

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

@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

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy