当前位置:网站首页>Leetcode (154) -- find the minimum value II in the rotation sort array
Leetcode (154) -- find the minimum value II in the rotation sort array
2022-07-02 18:42:00 【SmileGuy17】
Leetcode(154)—— Look for the minimum value in the rotation sort array II
subject
We know a length of n Array of , In ascending order in advance , Through 1 To n Time rotate after , Get the input array . for example , Original array nums = [0,1,4,4,5,6,7] After the change may get :
- If you rotate 4 Time , You can get [4,5,6,7,0,1,4]
- If you rotate 7 Time , You can get [0,1,4,4,5,6,7]
Be careful , Array [a[0], a[1], a[2], …, a[n-1]] Rotate once The result is an array [a[n-1], a[0], a[1], a[2], …, a[n-2]] .
Give you a chance to exist repeat An array of element values nums , It turns out to be an ascending array , According to the above situation, we have made many rotations . Please find and return the The smallest element .
You must minimize the steps of the whole process .
Example 1:
Input :nums = [1,3,5]
Output :1
Example 2:
Input :nums = [2,2,2,0,1]
Output :0
Tips :
- n == nums.length
- 1 1 1 <= n <= 5000 5000 5000
- − 5000 -5000 −5000 <= nums[i] <= 5000 5000 5000
- nums It turns out to be an ascending array , And carried on 1 1 1 to n n n Second rotation
Advanced : This problem is related to Look for the minimum value in the rotation sort array similar , but nums It may contain repeating elements . Does allowing repetition affect the time complexity of the algorithm ? How will it affect , Why? ?
Answer key
Method 1 : Two points search
Ideas
Rotate sort array n u m s nums nums Probably Split into 2 A sort array n u m s 1 , n u m s 2 nums1 , nums2 nums1,nums2, also n u m s 1 nums1 nums1 Any element > = n u m s 2 >= nums2 >=nums2 Any element ; therefore , Consider dichotomy to find the dividing point of these two arrays n u m s [ i ] nums[i] nums[i] ( That is to say 2 The first element of an array ).
Set up leftleft, rightright Pointer in numsnums Both ends of the array ,midmid It's the midpoint of every two minutes :
- When n u m s [ m i d ] > n u m s [ r i g h t ] nums[mid] > nums[right] nums[mid]>nums[right] when , m i d mid mid It must be in the 1 In a sorted array , i i i Must be satisfied with m i d < i < = r i g h t mid < i <= right mid<i<=right, So execute l e f t = m i d + 1 left = mid + 1 left=mid+1;
- When n u m s [ m i d ] < n u m s [ r i g h t ] nums[mid] < nums[right] nums[mid]<nums[right] when , m i d mid mid It must be in the 2 In a sorted array , i i i Must be satisfied with l e f t < i < = m i d left < i <= mid left<i<=mid, So execute r i g h t = m i d right = mid right=mid;
- When n u m s [ m i d ] = = n u m s [ r i g h t ] nums[mid] == nums[right] nums[mid]==nums[right] when , It's a comparison of this question 153 topic The difficulties of ( The reason is that the elements of the array in this question can be repeated , It's hard to judge the dividing point ii Pointer interval );
- for example [ 1 , 0 , 1 , 1 , 1 ] [1, 0, 1, 1, 1] [1,0,1,1,1] and [ 1 , 1 , 1 , 0 , 1 ] [1, 1, 1, 0, 1] [1,1,1,0,1], stay l e f t = 0 , r i g h t = 4 , m i d = 2 left = 0, right = 4, mid = 2 left=0,right=4,mid=2 when , Unable to judge m i d mid mid In which sort array .
- We use Make one end of the same end move the subscript until the new l l l and r r r The values of , To solve this problem ( That is to restore dichotomy ), prove :
- This operation will not make the array out of bounds : Because the iteration condition guarantees right > left >= 0;
- This operation will not cause the minimum value to be lost : hypothesis nums[right]nums[right] Is the minimum , There are two situations :
- if n u m s [ r i g h t ] nums[right] nums[right] Is the only minimum : Then it is impossible to meet the judgment conditions n u m s [ m i d ] = = n u m s [ r i g h t ] nums[mid] == nums[right] nums[mid]==nums[right], because
mid < right(left != right And mid = (left + right) // 2 Rounding down )
;
Code implementation
Leetcode Official explanation :
class Solution {
public:
int findMin(vector<int>& nums) {
int low = 0;
int high = nums.size() - 1;
while (low < high) {
int pivot = low + (high - low) / 2;
if (nums[pivot] < nums[high]) {
high = pivot;
}
else if (nums[pivot] > nums[high]) {
low = pivot + 1;
}
else {
high -= 1;
}
}
return nums[low];
}
};
My own :
class Solution {
public:
int findMin(vector<int>& nums) {
if(nums.size() == 1) return nums[0];
int l = 0, r = nums.size() - 1, mid;
// Subtract duplicate values , Restore secondary , But you can't delete them all , Keep the same number at least 1 individual ( and 81 The questions are different )
while(nums[l] == nums[r] && l < r) l++; // avoid [1,1]
while(l <= r){
mid = (l+r)/2;
if(nums[l] <= nums[r]) break;
else{
if(nums[mid] >= nums[l]) l = mid + 1; // Delete the left section
else r = mid; // Delete the right section
}
}
return nums[l];
}
};
Complexity analysis
Time complexity : Resuming secondary processing , At worst ( Consider that the whole array is the same number ) Complexity is O ( n ) O(n) O(n), The next step is to find the rotation point 「 Two points 」, The complexity is O ( l o g n ) O(log{n}) O(logn). The overall complexity is O ( n ) O(n) O(n) Of .
Spatial complexity : O ( 1 ) O(1) O(1)
边栏推荐
- UE4 用spline畫正圓
- Wechat nucleic acid detection appointment applet system graduation design (2) applet function
- Implementation shadow introduction
- 如何设置VSCode删除整行快捷键?
- options should NOT have additional properties
- cJSON 使用详解
- 又一所双非改考408,会爆冷么?南昌航空大学软件学院
- UE4 draw a circle with spline
- 一款简约PHP个人发卡程序V4.0版本
- Architecture design - ID generator "suggestions collection"
猜你喜欢
RDK simulation experiment
RDK仿真实验
ESP32-C3入门教程 问题篇⑩——error: implicit declaration of function ‘esp_blufi_close‘;
昨天阿里学长写了一个责任链模式,竟然出现了无数个bug
距离度量 —— 杰卡德距离(Jaccard Distance)
Web version 3D visualization tool, 97 things programmers should know, AI frontier paper | information daily # 2022.07.01
Leetcode(81)——搜索旋转排序数组 II
初夏,开源魔改一个带击杀音效的电蚊拍!
SteamOS 3.3 Beta 发布,Steam Deck 中文键盘终于来了
Uncover the whole link communication process of dewu customer service im
随机推荐
揭秘得物客服IM全链路通信过程
A simple PHP personal card issuing program v4.0
Meal card hdu2546
@Component 拿不到dao层
呆错图床系统源码图片CDN加速与破J防盗链功能
Matlab中弧度转角度、角度转弧度
Unity学习shader笔记[八十一]简单的颜色调整后处理(亮度,饱和度,对比度)
再放宽!这些应届生,可直接落户上海
Unity learning shader notes [82] black and white processing of enhanced single channel color rendering
如何清理废弃pv和其对应的文件夹
Vi/vim delete: one line, one character, word, the first character of each line command
PR曲线和ROC曲线概念及其区别
Export Excel files using npoi
Tower safety monitoring system unattended inclination vibration monitoring system
Memory mapping of QT
能解决80%故障的排查思路
微信核酸检测预约小程序系统毕业设计毕设(2)小程序功能
options should NOT have additional properties
元宇宙链游系统开发(逻辑开发)丨链游系统开发(详细分析)
CDN acceleration and breaking J anti-theft chain function