当前位置:网站首页>Find the minimum value in the rotation sort array ii[classical Abstract dichotomy + how to break the game left, middle and right are equal]
Find the minimum value in the rotation sort array ii[classical Abstract dichotomy + how to break the game left, middle and right are equal]
2022-06-27 00:56:00 【REN_ Linsen】
Classical Abstract dichotomy + How to break the game left, middle and right are equal
Preface
This question is a very classic Abstract dichotomy that I have done , Not only are decision rules abstract , And there will be decision rules “ Malfunction ” The phenomenon .
How to break the situation ? And mining the characteristics of a given array , Use your personality , For those routes ( positive / Converse thinking ), Break the problem , This is logic , Every cause has its effect , Use its cause to get its result , Not by chance .
One 、 Look for the minimum value in the rotation sort array II

Two 、 Classical Abstract dichotomy
package everyday.medium;
public class FindMin {
/* target: Find the minimum , Direct Abstract dichotomy , But notice before in When the last three are equal . Divide the rotated and non rotated subarrays into two subarrays . How to abstract ? Give Way nums[mid] and nums[0] Compare , If more than nums[0] be mid Must be in the first subarray ; If it is less than nums[0], that mid It must be in the second subarray . A special case : When nums[mid] = nums[0] when , Then look at nums[mid] and nums[high] The relationship between , if If you don't wait, you must nums[mid] > nums[high], that mid It must be in the first subarray . If so nums[mid] Also equal to nums[high] Na ? as follows , [2,2,2,2,2,2,2,0,1,2] | [2,2,2,0,1,2,2,2,2,2,2] How to break the situation ? Notice the personality of these two subarrays , It's all incremental ; The maximum and minimum values are next to each other , And the only logarithm , They are not incremental . When left, middle and right are equal , Then there is no hurry to take mid, can low Take a step forward , Break this three equal situation . if low Bit is the largest bit ? You can't low++! see nums[low] Is it greater than nums[low + 1], if ,nums[low] Must be the maximum value of the array , The minimum value follows immediately , Go straight back to . Why not? high-- To break the situation ? Because if high It is already in the maximum position ,high It can't move . Why not be like low and low + 1 To judge , Because both subarrays are incremented . The only non increasing pair can only be met from the front . */
public int findMin(int[] nums) {
// Find the maximum value in two , The minimum value will follow immediately , Abstract dichotomy .
int low = 0, high = nums.length - 1;
// Two points can be found quickly nums[0] Small , The first small one .
int first = nums[0];
while (low < high) {
int mid = low + (high - low + 1 >>> 1);// here +1 Very important , It needs to be rectified , With the following low = mid Prevent a dead cycle .
int midVal = nums[mid];
if (midVal > first) low = mid;
else if (midVal < first) high = mid - 1;
// The key to breaking the game .
else if (midVal == nums[high]) {
// Three important cases
// [3,3,1,3] [3,1,3,3] [2,2,2,0,2,2]
if (nums[low] <= nums[low + 1]) low++;
// The maximum and minimum values are next to each other , And the only logarithm , They are not incremental .
else return nums[low + 1];
}
// Can merge .
else low = mid;
}
return high + 1 == nums.length ? nums[0] : nums[high + 1];
}
}
summary
1) Classic Abstract dichotomy exercise .
2) Use its cause ( Title Display | Implicit ( Careful excavation is required ) Conditions / Personality characteristics ), Must have its fruit , Not by chance , This is logic .
reference
[1] LeetCode Look for the minimum value in the rotation sort array II
边栏推荐
- Lambda expression
- How to convert an old keyboard into a USB keyboard and program it yourself?
- Special topic II on mathematical physics of the sprint strong foundation program
- Hid device descriptor and keyboard key value corresponding coding table in USB protocol
- 高清滑环生产过程当中的质量如何把控
- MySQL之账号管理、建库以及四大引擎+案例
- CEC-I 中华学习机使用说明与问答
- 3线spi屏幕驱动方式
- 超越锂电池——未来电池的概念
- About Random Numbers
猜你喜欢
随机推荐
Lambda expression
【UVM实战 ===> Episode_3 】~ Assertion、Sequence、Property
这3个并发编程的核心,竟然还有人不知道?
Simple and fast digital network (network dolls in the network)
What are the skills and methods for slip ring installation
XML learning notes
Database interview questions +sql statement analysis
Statistical Hypothesis Testing
These 10 copywriting artifacts help you speed up the code. Are you still worried that you can't write a copywriting for US media?
ESP32-SOLO开发教程,解决CONFIG_FREERTOS_UNICORE问题
Timing mechanism of LwIP
超越锂电池——未来电池的概念
2022 Health Expo, Shandong health care exhibition, postpartum health and sleep health exhibition
find_ Detailed use guide of CIRC
指南针开户安全的吗?
How to write test cases and a brief introduction to go unit test tool testify
redis详细教程
Review the old and know the new -- constant renewal at normal temperature
Xiaobai looks at MySQL -- installing MySQL in Windows Environment
滑环安装有哪些技巧和方法








