HomeLeetcode154. Find Minimum in Rotated Sorted Array II - Leetcode Solutions

154. Find Minimum in Rotated Sorted Array II – Leetcode Solutions

Description

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:

Example 1:

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

Example 2:

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

Solution in Python

Python
from typing import List

class Solution:
    def findMin(self, nums: List[int]) -> int:
        # Initialize two pointers for the binary search
        left, right = 0, len(nums) - 1

        while left < right:
            # Calculate the mid index
            mid = (left + right) // 2
            
            # If the middle element is greater than the rightmost element,
            # the minimum must be in the right half (excluding mid)
            if nums[mid] > nums[right]:
                left = mid + 1
            # If the middle element is less than the rightmost element,
            # the minimum must be in the left half (including mid)
            elif nums[mid] < nums[right]:
                right = mid
            # If the middle element is equal to the rightmost element,
            # we cannot determine the exact position of the minimum,
            # but we can safely move the right pointer left by one
            else:
                right -= 1
        
        # When left meets right, we've found the minimum element
        return nums[left]

Explanation:

  1. Initialization:
    • Two pointers left and right are initialized to point to the start and end of the array, respectively.
  2. Binary Search Loop:
    • The loop continues until left is equal to right.
    • mid is calculated as the midpoint of the current left and right pointers.
  3. Comparison and Pointer Adjustment:
    • If nums[mid] is greater than nums[right], it indicates that the smallest value is in the right half of the array (excluding mid), so left is moved to mid + 1.
    • If nums[mid] is less than nums[right], it indicates that the smallest value is in the left half of the array (including mid), so right is moved to mid.
    • If nums[mid] is equal to nums[right], the exact position of the minimum is uncertain, but we can safely decrement right by one to narrow the search space.
  4. Result:
    • When the loop exits, left will be pointing to the minimum element in the array.

This approach ensures that the number of operations is minimized while handling the presence of duplicates effectively. The worst-case time complexity is O(n) when duplicates are present, but it performs significantly better on average due to the binary search approach.

Solution in Javascript

JavaScript
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMin = function(nums) {
    // Initialize two pointers for the binary search
    let left = 0, right = nums.length - 1;

    while (left < right) {
        // Calculate the mid index
        let mid = Math.floor((left + right) / 2);

        // If the middle element is greater than the rightmost element,
        // the minimum must be in the right half (excluding mid)
        if (nums[mid] > nums[right]) {
            left = mid + 1;
        // If the middle element is less than the rightmost element,
        // the minimum must be in the left half (including mid)
        } else if (nums[mid] < nums[right]) {
            right = mid;
        // If the middle element is equal to the rightmost element,
        // we cannot determine the exact position of the minimum,
        // but we can safely move the right pointer left by one
        } else {
            right--;
        }
    }

    // When left meets right, we've found the minimum element
    return nums[left];
};

Solution in Java

Java
class Solution {
    public int findMin(int[] nums) {
        // Initialize two pointers for the binary search
        int left = 0, right = nums.length - 1;

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

            // If the middle element is greater than the rightmost element,
            // the minimum must be in the right half (excluding mid)
            if (nums[mid] > nums[right]) {
                left = mid + 1;
            // If the middle element is less than the rightmost element,
            // the minimum must be in the left half (including mid)
            } else if (nums[mid] < nums[right]) {
                right = mid;
            // If the middle element is equal to the rightmost element,
            // we cannot determine the exact position of the minimum,
            // but we can safely move the right pointer left by one
            } else {
                right--;
            }
        }

        // When left meets right, we've found the minimum element
        return nums[left];
    }
}

Solution in C#

C#
public class Solution {
    public int FindMin(int[] nums) {
        // Initialize two pointers for the binary search
        int left = 0, right = nums.Length - 1;

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

            // If the middle element is greater than the rightmost element,
            // the minimum must be in the right half (excluding mid)
            if (nums[mid] > nums[right]) {
                left = mid + 1;
            // If the middle element is less than the rightmost element,
            // the minimum must be in the left half (including mid)
            } else if (nums[mid] < nums[right]) {
                right = mid;
            // If the middle element is equal to the rightmost element,
            // we cannot determine the exact position of the minimum,
            // but we can safely move the right pointer left by one
            } else {
                right--;
            }
        }

        // When left meets right, we've found the minimum element
        return nums[left];
    }
}

Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments

Popular