当前位置:网站首页>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
边栏推荐
- One click acceleration of Sony camera SD card file copy operation, file operation batch processing tutorial
- 如何把老式键盘转换成USB键盘并且自己编程?
- Using physical information neural network to solve hydrodynamics equations
- 史上最难618,TCL夺得电视行业京东和天猫份额双第一
- 气液滑环与其他滑环的工作原理有什么区别
- 07 | workflow design: how to design a reasonable multi person development mode?
- 网上开通证券账户安全吗 手机炒股靠谱吗
- 剑指 Offer 10- II. 青蛙跳台阶问题
- Play OLED, u8g2 animation, increasing numbers, random triangles, etc
- 墨者学院-SQL注入漏洞测试(报错盲注)
猜你喜欢
![[vscade] preview MD file](/img/b8/0413eaade0a7da9ddb5494b093665c.png)
[vscade] preview MD file
![The [MySQL] time field is set to the current time by default](/img/40/5f1d3448259ab703c4b5dc29713a99.png)
The [MySQL] time field is set to the current time by default

这3个并发编程的核心,竟然还有人不知道?

Central Limit Theorem

Statistical Hypothesis Testing

光谱共焦如何测量玻璃基板厚度

Hid device descriptor and keyboard key value corresponding coding table in USB protocol
![[vscode] setting sync, a plug-in for synchronizing extensions and settings](/img/e0/4889b59105e9815d11ae31988f58f2.jpg)
[vscode] setting sync, a plug-in for synchronizing extensions and settings

How to control the quality of HD slip ring in the production process

matlab数据类型 —— 字符型
随机推荐
Redis detailed tutorial
Is it safe for CITIC Securities Commission to open an online account and speculate in stocks
Is it safe to open a compass account?
解决u8glib只显示一行文字或者不显示的问题
Lwip之ARP模块实现
Esp32-solo development tutorial to solve config_ FREERTOS_ UNICORE problem
Simple and fast digital network (network dolls in the network)
When transformer encounters partial differential equation solution
BootstrapBlazor + FreeSql实战 Chart 图表使用(2)
LeetCode 142. Circular linked list II
Other service registration and discovery
Central Limit Theorem
Com. Faster XML. Jackson. DataBind. Exc.mismatchedinputexception: tableau ou chaîne attendu. At [Source: X
Moher College -x-forwarded-for injection vulnerability practice
MATLAB data type - character type
Solve the problem that only one line of text is displayed or not displayed in u8glib
3 - wire SPI Screen Drive
CPU exception handling
MySQL之账号管理、建库以及四大引擎+案例
Oracle 数据库基本知识概念