LeetCode //C - 153. Find Minimum in Rotated Sorted Array

153. Find Minimum in Rotated Sorted Array

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

  • 4,5,6,7,0,1,2\] if it was rotated 4 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 of unique elements, return the minimum element of this array.

You must write an algorithm that runs in O(log n) time.

Example 1:

Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.

Example 2:

Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.

Example 3:

Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.

Constraints:
  • n == nums.length
  • 1 <= n <= 5000
  • -5000 <= nums[i] <= 5000
  • All the integers of nums are unique.
  • nums is sorted and rotated between 1 and n times.

From: LeetCode

Link: 153. Find Minimum in Rotated Sorted Array


Solution:

Ideas:

1. Binary Search:

The array is sorted but rotated. If we pick the middle element, we can determine which half of the array is strictly increasing and which half contains the rotation point (and hence the minimum).

2. Identifying the Correct Half:

  • If the middle element is greater than the rightmost element, the rotation point (and minimum) must be to the right of the middle. For example, in [4, 5, 6, 7, 0, 1, 2], picking the middle element 7 shows that the array is not strictly increasing from mid to right, so we search in the right half.
  • If the middle element is less than or equal to the rightmost element, the rotation point is in the left half (including the middle element). For example, in [5, 6, 7, 0, 1, 2, 4], picking the middle element 0 shows that the array is strictly increasing from mid to right, so we search in the left half.

3. Finding the Minimum:

Continue the binary search until the left and right pointers converge, at which point they will point to the minimum element in the array.

4. Time Complexity:

The binary search cuts the search space in half at each step, resulting in an O(logn) time complexity, where n is the size of the array.

5. Implementation:

  • Initialize two pointers, left and right, to the start and end of the array, respectively.
  • Repeat the following until left < right:
    • Calculate the middle index.
    • If the middle element is greater than the rightmost element, update left to mid + 1.
    • Otherwise, update right to mid.
  • When left == right, the pointers have converged to the minimum element. Return nums[left].
Code:
c 复制代码
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]) {
            // If mid element is greater than the rightmost element,
            // the minimum must be in the right part
            left = mid + 1;
        } else {
            // If mid element is less than or equal to the rightmost element,
            // the minimum must be in the left part including the mid element
            right = mid;
        }
    }
    
    // When the while loop ends, left == right, pointing to the minimum element
    return nums[left];
}
相关推荐
未来之窗软件服务5 小时前
自己写算法(九)网页数字动画函数——东方仙盟化神期
前端·javascript·算法·仙盟创梦ide·东方仙盟·东方仙盟算法
豐儀麟阁贵5 小时前
基本数据类型
java·算法
程序员老舅7 小时前
干货|腾讯 Linux C/C++ 后端开发岗面试
linux·c语言·c++·编程·大厂面试题
乐迪信息7 小时前
乐迪信息:基于AI算法的煤矿作业人员安全规范智能监测与预警系统
大数据·人工智能·算法·安全·视觉检测·推荐算法
hsjkdhs8 小时前
C++之多层继承、多源继承、菱形继承
开发语言·c++·算法
立志成为大牛的小牛8 小时前
数据结构——十七、线索二叉树找前驱与后继(王道408)
数据结构·笔记·学习·程序人生·考研·算法
星空下的曙光8 小时前
Node.js crypto模块所有 API 详解 + 常用 API + 使用场景
算法·node.js·哈希算法
StarPrayers.10 小时前
旅行商问题(TSP)(2)(heuristics.py)(TSP 的两种贪心启发式算法实现)
前端·人工智能·python·算法·pycharm·启发式算法
qiuiuiu41310 小时前
正点原子RK3568学习日志-编译第一个驱动程序helloworld
linux·c语言·开发语言·单片机
爱吃橘的橘猫10 小时前
嵌入式系统与嵌入式 C 语言(2)
c语言·算法·嵌入式