当前位置:网站首页>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)
边栏推荐
- Tower safety monitoring system unattended inclination vibration monitoring system
- 再放宽!这些应届生,可直接落户上海
- Vi/vim delete: one line, one character, word, the first character of each line command
- Meta universe chain game system development (logic development) - chain game system development (detailed analysis)
- Esp32-c3 introductory tutorial question ⑩ - error: implicit declaration of function 'ESP_ blufi_ close‘;
- 饭卡 HDU2546
- UE4 draw a circle with spline
- “栈”的典型应用—表达式求值(C语言实现)
- 呆错图床系统源码图片CDN加速与破J防盗链功能
- Server PHP environment building tutorial, PHP server environment building graphic explanation
猜你喜欢

再放寬!這些應届生,可直接落戶上海

Redis (6) -- object and data structure

实施阴影介绍

What is cloud primordial? This time, I can finally understand!

Web版3D可视化工具,程序员应该知道的97件事,AI前沿论文 | 资讯日报 #2022.07.01

Qt官方示例:Qt Quick Controls - Gallery
![[Yugong series] July 2022 go teaching course 001 introduction to go language premise](/img/f2/3b95f53d67cd1d1979163910dbeeb8.png)
[Yugong series] July 2022 go teaching course 001 introduction to go language premise

A good programmer is worth five ordinary programmers!

Wechat applet video sharing platform system graduation design completion (6) opening defense ppt

揭秘得物客服IM全链路通信过程
随机推荐
Wechat nucleic acid detection appointment applet system graduation design completion (5) task statement
Which securities company has a low, safe and reliable online account opening commission
Export Excel files using npoi
In early summer, Kaiyuan magic changed an electric mosquito racket with killing sound effect!
Web chat tool
SLAM|如何时间戳对齐?
Leetcode 面试题 16.17. 连续数列
任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
Meta universe chain game system development (logic development) - chain game system development (detailed analysis)
RTE11- 中断解耦功能
QQmlApplicationEngine
怎么用ps提取图片颜色分析色彩搭配
Chrome officially supports MathML, which is enabled in chromium dev 105 by default
Detailed explanation of cjson usage
How to set vscode to delete the whole line shortcut key?
ESP32-C3入门教程 问题篇⑪——esp-tls: create_ssl_handle failed, tls_io_instance->options.trusted_certs null
Meal card hdu2546
故障排查:kubectl报错ValidationError: unknown field \u00a0
Wechat applet video sharing platform system graduation design completion (4) opening report
如何优雅的写 Controller 层代码?