当前位置:网站首页>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)
边栏推荐
- 怎么用ps提取图片颜色分析色彩搭配
- Nm02 nm module call sequence diagram and code interpretation independent of bus protocol
- 国金证券是国企吗?在国金证券开户资金安全吗?
- Redis(6)----对象与数据结构
- Leetcode 面试题 16.17. 连续数列
- Pit encountered during installation of laravel frame
- PR曲线和ROC曲线概念及其区别
- Detailed explanation of cjson usage
- 如何优雅的写 Controller 层代码?
- Typical application of "stack" - expression evaluation (implemented in C language)
猜你喜欢
27:第三章:开发通行证服务:10:【注册/登录】接口:注册/登录OK后,把用户会话信息(uid,utoken)保存到redis和cookie中;(一个主要的点:设置cookie)
A simple PHP personal card issuing program v4.0
Leetcode interview question 16.17 Continuous sequence
Redis(6)----对象与数据结构
Implementation shadow introduction
Troubleshooting ideas that can solve 80% of faults
Summary of fun free GM games
In early summer, Kaiyuan magic changed an electric mosquito racket with killing sound effect!
Web version 3D visualization tool, 97 things programmers should know, AI frontier paper | information daily # 2022.07.01
Responses of different people in technology companies to bugs | daily anecdotes
随机推荐
How to write controller layer code gracefully?
Chain game system development (unity3d chain game development details) - chain game development mature technology source code
C语言中函数参数传递的三种方式
工业软件讲堂-三维CAD设计软件的核心技术解析----讲坛第二次讲座
ESP32-C3入门教程 问题篇⑪——esp-tls: create_ssl_handle failed, tls_io_instance->options.trusted_certs null
Please, stop painting star! This has nothing to do with patriotism!
@Component 拿不到dao层
揭秘得物客服IM全链路通信过程
[Northwestern Polytechnic University] information sharing of the first and second postgraduate examinations
sql训练2
Uncover the whole link communication process of dewu customer service im
iptable端口重定向 MASQUERADE[通俗易懂]
距离度量 —— 杰卡德距离(Jaccard Distance)
Détends - toi encore! Ces nouveaux étudiants peuvent s'installer directement à Shanghai
Troubleshooting ideas that can solve 80% of faults
Wechat applet video sharing platform system graduation design completion (7) Interim inspection report
Wechat nucleic acid detection appointment applet system graduation design completion (1) development outline
UE4 draw a circle with spline
Aptos tutorial - participate in the official incentive testing network (ait2 incentive testing network)
After 22 years in office, the father of PowerShell will leave Microsoft: he was demoted by Microsoft for developing PowerShell