โ† Back to Index

๐Ÿ“š Find Minimum in Rotated Sorted Array II โ€“ Java Cheat Sheet

๐Ÿ“Œ What Is It?

The Find Minimum in Rotated Sorted Array II problem involves finding the minimum element in a rotated sorted array that may contain duplicates. The goal is to solve this in O(log N) time in the best case, but it may degrade to O(N) in the presence of duplicates.

๐Ÿงฑ Pattern Template

class Solution {
    public int findMin(int[] nums) {
        int left = 0, right = nums.length - 1;

        while (left < right) {
            int mid = left + (right - left) / 2;

            if (nums[mid] < nums[right]) {
                right = mid; // Minimum is in the left half
            } else if (nums[mid] > nums[right]) {
                left = mid + 1; // Minimum is in the right half
            } else {
                right--; // Handle duplicates by reducing the search space
            }
        }

        return nums[left]; // Minimum element
    }
}

๐Ÿ“Š Time Complexity

โœ… Use Cases

๐Ÿ“˜ Common LeetCode Problems

๐Ÿงช Example: Find Minimum in Rotated Sorted Array II

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

Explanation:
- The minimum element in the array is 0.

๐Ÿ’ก Pro Tips