当前位置:网站首页>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
边栏推荐
- The [MySQL] time field is set to the current time by default
- Law of Large Numbers
- Review the old and know the new -- constant renewal at normal temperature
- redis详细教程
- Database interview questions +sql statement analysis
- 2022健康博览会,山东养生保健展会,产后健康、睡眠健康展
- Unable to create a folder to save the sketch: MKDIR sketch
- [vscode] setting sync, a plug-in for synchronizing extensions and settings
- XML learning notes
- Xiaobai looks at MySQL -- installing MySQL in Windows Environment
猜你喜欢
![Custom jsp[if, foreach, data, select] tag](/img/a2/fc75c182d572d86f4466323e31d6c3.png)
Custom jsp[if, foreach, data, select] tag

Statistical Hypothesis Testing

2022年地理信息系统与遥感专业就业前景与升学高校排名选择

Mindspire, a domestic framework, cooperates with Shanshui nature conservation center to find and protect the treasure life in the "China water tower"

一键加速索尼相机SD卡文件的复制操作,文件操作批处理教程

com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x

Com. Faster XML. Jackson. DataBind. Exc.mismatchedinputexception: tableau ou chaîne attendu. At [Source: X

Live review | Ziya &ccf TF: Discussion on software supply chain risk management technology under cloud native scenario

【UVM实战 ===> Episode_3 】~ Assertion、Sequence、Property

Employment prospect of GIS and remote sensing specialty and ranking selection of universities in 2022
随机推荐
Oracle 數據庫基本知識概念
Great health industry annual must attend event, 2022 Shandong International Great Health Industry Expo
Competition Registration | one of the key ai+ scientific computing competitions - China open source scientific software creativity competition, competing for 100000 bonus!
记录一次换行符引起的bug
简单快速的数网络(网络中的网络套娃)
Timing mechanism of LwIP
Solve the problem that stc8g1k08 program cannot run and port configuration
Skills needing attention in selection and purchase of slip ring
JSON parsing, esp32 easy access to time, temperature and weather
How to write test cases and a brief introduction to go unit test tool testify
3 - wire SPI Screen Drive
Play OLED, u8g2 animation, increasing numbers, random triangles, etc
Employment prospect of GIS and remote sensing specialty and ranking selection of universities in 2022
JSON解析,ESP32轻松获取时间气温和天气
What is the difference between the working principle of gas-liquid slip ring and other slip rings
How to use ch423? Cheap domestic IO expansion chip
find_circ详细使用指南
Encapsulation of unified result set
[vscade] preview MD file
Database interview questions +sql statement analysis