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.
  • 0,1,2,4,5,6,7 if it was rotated 7 times.

Notice that rotating an array a\[0, a1, a2, ..., an-1] 1 time results in the array a\[n-1, a0, a1, a2, ..., an-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 <= numsi <= 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 numsleft.
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];
}
相关推荐
小白兔奶糖ovo33 分钟前
【Leetcode】231. 2的幂
linux·算法·leetcode
xiaoxiaoxiaolll36 分钟前
《Light: Science & Applications》合并BIC实现80倍阈值单模运行:超紧凑光子晶体激光器新突破
人工智能·算法·机器学习
Peter·Pan爱编程42 分钟前
14. Lambda 表达式:随手可写的函数对象
c++·算法·ai编程
-To be number.wan42 分钟前
算法日记 | 暴力枚举
学习·算法
s_w.h1 小时前
【 linux 】动静态库的制作
linux·运维·服务器·算法·bash
过期动态2 小时前
【LeetCode 热题 100】接雨水
java·数据结构·算法·leetcode·职场和发展
春日见2 小时前
5分钟入门强化学习之动态规划算法与实现
大数据·人工智能·python·算法·机器学习·计算机视觉
一抹晴空2 小时前
Keil MDK AC6 compiler编译报错,与AC5区别
c语言·arm开发·单片机
scx_link2 小时前
线性回归的总结:
算法·机器学习·线性回归
郝亚军2 小时前
IEEE 754 单精度浮点的SEM表示
开发语言·c++·算法