> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find Minimum in Rotated Sorted Array II

> Tested Python solution for LeetCode 154 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 154, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search). [View on LeetCode](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 154   # by problem number
lcpy gen -s find_minimum_in_rotated_sorted_array_ii   # by problem name
```

## Problem

Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,4,4,5,6,7]` might become:

* `[4,5,6,7,0,1,4]` if it was rotated `4` times.
* `[0,1,4,4,5,6,7]` if it was rotated `7` times.

Notice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`.

Given the sorted rotated array `nums` that may contain **duplicates**, return *the minimum element of this array*.

You must decrease the overall operation steps as much as possible.

### Examples

```
Input: nums = [1,3,5]
Output: 1
```

```
Input: nums = [2,2,2,0,1]
Output: 0
```

### Constraints

* n == nums.length
* 1 \<= n \<= 5000
* -5000 \<= nums\[i] \<= 5000
* nums is sorted and rotated between 1 and n times.

**Follow up:** This problem is similar to Find Minimum in Rotated Sorted Array, but `nums` may contain duplicates. Would this affect the runtime complexity? How and why?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_minimum_in_rotated_sorted_array_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_minimum_in_rotated_sorted_array_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log n) average, O(n) worst case with all-duplicate stretches
    # Space: O(1)
    def find_min(self, nums: list[int]) -> int:
        left, right = 0, len(nums) - 1
        while left < right:
            mid = (left + right) // 2
            if nums[mid] > nums[right]:
                left = mid + 1
            elif nums[mid] < nums[right]:
                right = mid
            else:
                right -= 1
        return nums[left]
```

## Complexity

| Time | Space |
| - | - |
| O(log n) average, O(n) worst case with all-duplicate stretches | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.