首页 > 其他分享 >*153. Find Minimum in Rotated Sorted Array[Medium]

*153. Find Minimum in Rotated Sorted Array[Medium]

时间:2023-01-29 17:55:45浏览次数:45  
标签:153 Medium min rotated nums Rotated mid array left

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.

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.

Example

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

思路

二分查找变体,虽然被旋转了,但子串还是有序的,通过中位来判断

题解

    public int findMin(int[] nums) {
        int left, right, mid, min;
        left = 0;
        right = nums.length - 1;
        min = nums[left];

        while (left <= right) {
            // 如果当前最右比最左大,那说明这一段子串是顺序的,直接取最左
            if (nums[right] > nums[left]) {
                min = Math.min(min, nums[left]);
                break;
            }

            mid = (left + right) / 2;
            min = Math.min(nums[mid], min);

            // 如果当前中位比最右大,那说明右边这段应该是被旋转了,是属于小的那一部分,就切到右半边来
            if (nums[mid] > nums[right])
                left = mid + 1;
            else
                // 如果当前中位比最右小,那没有旋转,左边是小的部分,切到左半边
                right = mid - 1;

        }
        return min;
    }

标签:153,Medium,min,rotated,nums,Rotated,mid,array,left
From: https://www.cnblogs.com/tanhaoo/p/17073347.html

相关文章

  • ARC153F - Tri-Colored Paths
    题意给定一个\(n\)个点\(m\)条边的无向连通图,求将\(m\)条边进行\(3\)染色且满足:存在一条简单路径,使得路径上三种颜色的边各有至少一条。的方案数。数据范围:\(......
  • 1538 迎春舞会之数字舞蹈 题解
    #include<iostream>intmain(){/**#Seven-segmentDisplay**Thewayhowtheprogramprintsdecimalnumericstotheconsoleworks......
  • ZROJ370 Medium Counting - 区间dp -
    题目链接:http://zhengruioi.com/problem/370题解:考虑对于\(S[l..r]\),如果要符合条件必然是在最高位分成了至少两段(也可能没有分出来,那就继续下一位)\(S[l..k]和S[k+1......
  • UVA11538 Chess Queen
    简要题意给你一个\(n\timesm\)的棋盘,你需要在棋盘上放置两个颜色不同的皇后,使得它们互相攻击。求方案数。\(1\leqn,m\leq10^6\)思路下面假设\(n\leqm\)。首......
  • LeetCode Top 100 Liked Questions 64. Minimum Path Sum (Java版; Medium)
    ​​welcometomyblog​​LeetCodeTop100LikedQuestions64.MinimumPathSum(Java版;Medium)题目描述Givenamxngridfilledwithnon-negativenumbers,fi......
  • LeetCode Top 100 Liked Questions 62. Unique Paths (Java版; Medium)
    ​​welcometomyblog​​LeetCodeTop100LikedQuestions62.UniquePaths(Java版;Medium)题目描述Arobotislocatedatthetop-leftcornerofamxngrid(......
  • CF1153F Serval and Bonus Problem
    2022.08.06CF1153FServalandBonusProblem洛谷:CF1153FCodeforces:CF1153FSolution1首先指明,随机出来的两个点重合的概率很小,我们忽略不计。那么我们会随机出来\(......
  • AtCoder Regular Contest 153(持续更新)
    PrefaceB题粗糙了改了好几个版本,最后索性从头理了一遍思路才过然后剩下40min想C又歪了(构造题精通的被动消失了),还剩25min的时候忍不住了看LPL去了话说现在的ARC感觉和以......
  • 题解 ARC153B【Grid Rotations】
    一次操作是把四个子矩形分别旋转\(180^\circ\)。不难发现,可以横纵坐标分别考虑,则该操作变为横坐标的两段区间分别翻转、纵坐标的两段区间分别翻转。区间翻转操作、最后输......
  • Atcoder Regular Contest ARC 153 A B C D 题解
    点我看题A-AABCDDEFE一个beautifulnumber是形如这样的:\(S1S1S3S4S5S5S7S8S7\)。如果选定了\(S1\),后面的数有100000种选法,所以先求出答案的\(S1\)。假设现在我们要求出......