LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
The array may contain duplicates.
SOLUTION 1:
请参考前一个题目Find Minimum in Rotated Sorted Array
1. 如何找中间断开的区间(也就是说旋转过)
我们的目的是要找出存在断口的地方。所以我们可以每次求一下mid的值,把mid
跟左边比一下,如果是正常序,就丢掉左边,反之丢掉右边,不断反复直到找到断口。
分析一下:
比如4 5 6 7 0 1 2 从中间断开后,它是由一个有序跟一个无序的序列组成的。
如果left = 0, right = 6,mid = 3, 那么4, 5, 6, 7 是正序, 7, 0, 1,
2是逆序,所以我们要丢掉左边。这样反复操作,直到数列中只有2个数字,就是断开处,这题我们会得到7,0,返回后一个就可以了。
以下图示简单描述了通过三步操作逐步逼近断口处。每一次我们可以扔掉一半,速度是LogN.
2. 特别的情况:
如果发现 A.mid > A.left,表示左边是有序,丢掉左边。
如果发现 A.mid < A.left, 表示无序的状态在左边,丢掉右边
如果A.mid = A.left,说明无法判断。这时我们可以把left++,丢弃一个即可。不必担心丢掉我们的目标值。因为A.left == A.mid,即使丢掉了left,还有mid在嘛!
每次进入循环,我们都要判断A.left < A.right,原因是,前面我们丢弃一些数字时,有可能造成余下的数组是有序的,这时应直接返回A.left! 否则的话 我们可能会丢掉解。
就像以下的例子,在1 10 10中继续判断会丢弃1 10.
举例: 10 1 10 10 如果我们丢弃了最左边的10,则1 10 10 是有序的
3.对复杂度的影响:
题目中问到了,对复杂度有何影响:实际上是有的,如果全部的数字相等,我们就退化为O(N),但是平均的复杂度仍然是O(LogN),最后复杂度的大小取决于重复的数字的多少。如果重复字数少,与logN相差不大。
public class Solution {
public int findMin(int[] num) {
if (num == null || num.length == 0) {
return 0;
}
int len = num.length;
if (len == 1) {
return num[0];
} else if (len == 2) {
return Math.min(num[0], num[1]);
}
int left = 0;
int right = len - 1;
while (left < right - 1) {
int mid = left + (right - left) / 2;
// In this case, the array is sorted.
// 这一句很重要,因为我们移除一些元素后,可能会使整个数组变得有序...
if (num[left] < num[right]) {
return num[left];
}
// left side is sorted. CUT the left side.
if (num[mid] > num[left]) {
left = mid;
// left side is unsorted, right side is sorted. CUT the right side.
} else if (num[mid] < num[left]) {
right = mid;
} else {
left++;
}
}
return Math.min(num[left], num[right]);
}
}
2015.1.1 Redo:
public class Solution {
public int findMin(int[] num) {
if (num == null || num.length == ) {
return ;
}
int l = ;
int r = num.length - ;
while (l < r - ) {
int mid = l + (r - l) / ;
// The array is sorted.
if (num[l] < num[r]) {
return num[l];
}
if (num[mid] < num[r]) {
r = mid;
// left side is sorted. discard the left side.
} else if (num[mid] > num[l]) {
l = mid;
} else {
l++;
}
}
return Math.min(num[l], num[r]);
}
}
GitHub Code:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/binarySearch/FindMin2.java
LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法的更多相关文章
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...
- 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告
题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...
- 【刷题-LeetCode】154 Find Minimum in Rotated Sorted Array II
Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
- LeetCode OJ 154. Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
- Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...
随机推荐
- urlretrieve 如何给文件下载设置下载进度?
#python #xiaodeng #如何给文件下载设置下载进度? import urllib def callbackinfo(down,block,size): ''' 回调函数: down:已经 ...
- Python pycharm(windows版本)部署spark环境
一 部署本地spark环境 1.1 安装好JDK 下载并安装好jdk1.7,配置完环境变量. 1.2 Spark环境变量配置 去http://spark.apache.o ...
- 判断是否为SIM卡联系人
判断是否为SIM卡联系人 在AsyncQueryContacts类中. private List<TxrjAccount> accounts = new ArrayList<Txrj ...
- 【微信小程序】解决 竖向<scroll-view>组件 “竖向滚动页面出现遮挡”问题
问题图: 问题原因: <scroll-view class="scroll-container" upper-threshold="{{sortPanelDist} ...
- MySQL5.7 二进制源码包安装
一般平时安装MySQL都是源码包安装的,但是由于它的编译需要很长的时间,所以建议安装二进制免编译包.可以到MySQL官方网站去下载,也可以到comsenz官方网站下载,还有各大镜像站下载. 下载安装包 ...
- 配置eclipse插件
http://blog.csdn.net/zhangyabinsky/article/details/7043435
- nginx error: upstream prematurely closed connection while reading response header from upstream
本篇文章由:http://xinpure.com/nginx-error-upstream-prematurely-closed-connection-while-reading-response-h ...
- HDUOJ---The Moving Points
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Java中的平衡树
leetcode 729 给定一堆线段,每个线段都有一个起点.一个终点,用数组[(beg1,end1),(beg2,end2),(beg3,end3)......]来表示.可以提出以下问题: 这些线段 ...
- MFC中创建多线程
1. 列举几种进程的同步机制,并比较其优缺点. 原子操作 信号量机制 自旋锁 管程,会合,分布式系统 2. 进程之间通信的途径 共享存储系统 消息传递系统 ...