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

这篇具有很好参考价值的文章主要介绍了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], 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:文章来源地址https://www.toymoban.com/news/detail-741738.html

  • 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:
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];
}

到了这里,关于LeetCode //C - 153. Find Minimum in Rotated Sorted Array的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode2111. Minimum Operations to Make the Array K-Increasing——动态规划

    You are given a 0-indexed array arr consisting of n positive integers, and a positive integer k. The array arr is called K-increasing if arr[i-k] = arr[i] holds for every index i, where k = i = n-1. For example, arr = [4, 1, 5, 2, 6, 2] is K-increasing for k = 2 because: arr[0] = arr[2] (4 = 5) arr[1] = arr[3] (1 = 2) arr[2] = arr[4] (5 = 6) arr[3] = arr[5]

    2024年03月09日
    浏览(43)
  • 【LeetCode 算法】Minimum Operations to Halve Array Sum 将数组和减半的最少操作次数-Greedy

    给你一个正整数数组 nums 。每一次操作中,你可以从 nums 中选择 任意 一个数并将它 减小 到 恰好 一半 。(注意,在后续操作中你可以对减半过的数继续执行操作) 请你返回将 nums 数组和 至少 减少一半 的 最少 操作数。 1 = n u m s . l e n g t h = 1 0 5 1 = n u m s [ i ] = 1 0 7 1 = num

    2024年02月15日
    浏览(46)
  • 【递增栈】个人练习-Leetcode-845. Longest Mountain in Array

    题目链接:https://leetcode.cn/problems/longest-mountain-in-array/ 题目大意:给出一个数组 arr[] ,定义【山数组】为【长度大于等于3】【前面部分递增,后面部分递减,存在一个山峰元素】的数组。求 arr[] 中的最长的是【山数组】的子列。 思路:递增栈当然可以,不过刚好想到另一个

    2024年02月06日
    浏览(53)
  • leetcode - 852. Peak Index in a Mountain Array

    An array arr a mountain if the following properties hold: You must solve it in O(log(arr.length)) time complexity. Example 1: Example 2: Example 3: Constraints: Use binary search to solve this problem. For any middle index, it either locates at the left of the peak, or the right of the peak. If at the left, then discard the left half, otherwise discard the r

    2024年02月15日
    浏览(36)
  • LeetCode 2475. Number of Unequal Triplets in Array【数组,排序,哈希表】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(46)
  • LeetCode 2496. Maximum Value of a String in an Array【字符串,数组】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月11日
    浏览(60)
  • LeetCode --- 1971. Find if Path Exists in Graph 解题报告

    There is a  bi-directional  graph with  n  vertices, where each vertex is labeled from  0  to  n - 1  ( inclusive ). The edges in the graph are represented as a 2D integer array  edges , where each  edges[i] = [ui, vi]  denotes a bi-directional edge between vertex  ui  and vertex  vi . Every vertex pair is connected by  at most one  edge, and

    2024年02月07日
    浏览(44)
  • LeetCode 833. Find And Replace in String【字符串,哈希表,模拟】1460

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月12日
    浏览(44)
  • 【算法】Two Sum II - Input Array Is Sorted 两数之和 II - 输入有序数组

    给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列 ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1] 和 numbers[index2] ,则 1 = index1 index2 = numbers.length 。 以长度为 2 的整数数组 [index1, index2] 的形式返回这两个

    2024年02月13日
    浏览(50)
  • 【Python】成功解决ValueError: zero-size array to reduction operation minimum which has no identity

    【Python】成功解决ValueError: zero-size array to reduction operation minimum which has no identity 🌈 个人主页:高斯小哥 🔥 高质量专栏:Matplotlib之旅:零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程👈 希望得到您的订阅和支持~ 💡 创作高质量博文(平均质量分9

    2024年04月16日
    浏览(59)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包