LeetCode //C - 154. Find Minimum in Rotated Sorted Array II

154. Find Minimum in Rotated Sorted Array II

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.
 

Example 1:

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

Example 2:

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.

From: LeetCode
Link: 154. Find Minimum in Rotated Sorted Array II


Solution:

Ideas:

1. Initialization: Set two pointers, left and right, at the beginning and end of the array, respectively.

2. While Loop: Continue searching as long as left is less than right.

3. Middle Element: Calculate the middle position mid between left and right.

4. Decision Tree:

  • If nums[mid] is greater than nums[right], the minimum is in the right half (excluding mid), so move left to mid + 1.
  • If nums[mid] is less than nums[right], the minimum could be mid or to the left of mid, so move right to mid.
  • If nums[mid] equals nums[right], reduce right by one to gradually eliminate duplicates without skipping the minimum.

5. Conclusion: Once left equals right, the minimum element is found, as the search space is narrowed down to a single element.

Code:
int findMin(int* nums, int numsSize) {
    int left = 0, right = numsSize - 1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[right]) {
            left = mid + 1;
        } else if (nums[mid] < nums[right]) {
            right = mid;
        } else { // nums[mid] == nums[right]
            right--;
        }
    }
    return nums[left];
}

相关推荐

  1. 面试经典150题(14)

    2024-04-06 05:16:12       62 阅读
  2. Acwing154滑动窗口

    2024-04-06 05:16:12       53 阅读
  3. 面试经典150题(101-104)

    2024-04-06 05:16:12       46 阅读
  4. LeetCode 面试经典150134.加油站

    2024-04-06 05:16:12       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-06 05:16:12       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-06 05:16:12       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-06 05:16:12       87 阅读
  4. Python语言-面向对象

    2024-04-06 05:16:12       96 阅读

热门阅读

  1. 【电路笔记】-逻辑或门

    2024-04-06 05:16:12       36 阅读
  2. 掌握ChatGPT技巧,写出拔尖学术论文

    2024-04-06 05:16:12       33 阅读
  3. 【算法集训】基础算法:前缀和 | 习题篇

    2024-04-06 05:16:12       39 阅读
  4. 【小说拉期待感的几个小技巧】

    2024-04-06 05:16:12       45 阅读
  5. PyTorch搭建Informer实现长序列时间序列预测

    2024-04-06 05:16:12       33 阅读
  6. Mysql不同条件设置相同的值(使用子查询)

    2024-04-06 05:16:12       42 阅读
  7. AI大模型学习

    2024-04-06 05:16:12       35 阅读
  8. 文字识别 Optical Character Recognition,OCR CTC STN

    2024-04-06 05:16:12       47 阅读
  9. Docker in Docker原理与实战

    2024-04-06 05:16:12       44 阅读
  10. “头痛医头、脚痛医脚”的SAP解决方案

    2024-04-06 05:16:12       38 阅读