当前位置:网站首页>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)
边栏推荐
- Radian to angle, angle to radian in MATLAB
- Ue4 dessine un cercle avec une ligne de contour
- 消除IBM P750小机上的黄色报警灯[通俗易懂]
- Leetcode(81)——搜索旋转排序数组 II
- 如何设置VSCode删除整行快捷键?
- Wechat nucleic acid detection appointment applet system graduation design completion (1) development outline
- Is Guojin securities a state-owned enterprise? Is it safe to open an account in Guojin securities?
- Win10 uninstall CUDA
- Wechat applet video sharing platform system graduation design completion (6) opening defense ppt
- The text editor hopes to mark the wrong sentences in red, and the text editor uses markdown
猜你喜欢
文字编辑器 希望有错误的句子用红色标红,文字编辑器用了markdown
UE4 用spline画正圆
Another double non reform exam 408, will it be cold? Software College of Nanchang Aviation University
Wechat applet video sharing platform system graduation design completion (6) opening defense ppt
Leetcode 面试题 16.11. 跳水板
Please, stop painting star! This has nothing to do with patriotism!
夜神模拟器+Fiddler抓包测试App
Chrome 正式支持 MathML,默认在 Chromium Dev 105 中启用
Qt官方示例:Qt Quick Controls - Gallery
Simulateur nightGod + application de test de capture de paquets Fiddler
随机推荐
RDK仿真实验
饭卡 HDU2546
@Component cannot get Dao layer
任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
CDN acceleration and breaking J anti-theft chain function
距离度量 —— 杰卡德距离(Jaccard Distance)
如何清理废弃pv和其对应的文件夹
Leetcode 面试题 16.17. 连续数列
哪个券商公司网上开户佣金低又安全又可靠
Which securities company has a low, safe and reliable online account opening commission
夜神模拟器+Fiddler抓包测试App
ESP32-C3入门教程 问题篇⑩——error: implicit declaration of function ‘esp_blufi_close‘;
Wechat applet video sharing platform system graduation design completion (8) graduation design thesis template
Basic idea of quick sorting (easy to understand + examples) "suggestions collection"
QT official example: QT quick controls - Gallery
The official docker image running container in version 1.5.1 can be set to use MySQL 8 driver?
Wechat nucleic acid detection appointment applet system graduation design completion (1) development outline
27:第三章:开发通行证服务:10:【注册/登录】接口:注册/登录OK后,把用户会话信息(uid,utoken)保存到redis和cookie中;(一个主要的点:设置cookie)
[Oracle final review] addition, deletion and modification of tablespaces, tables, constraints, indexes and views
Unity学习shader笔记[八十二]增强单通道颜色渲染的黑白处理