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 解题报告-二分法模板解法的更多相关文章

  1. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

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

    Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...

  3. 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告

    题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...

  4. 【刷题-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 ...

  5. 【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 ...

  6. 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 ...

  7. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  8. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

  9. Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)

    Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...

随机推荐

  1. struts2接收参数的几种形式

    1.使用属性 HTML: <form action="login" method="post" name="form1"> 用户 ...

  2. CentOS 6.4 配置DNS

    vi /etc/resolv.conf 写入以下内容并保存: nameserver x.x.x.x 重启服务以生效: service network restart

  3. 通俗的理解HTTPS以及SSL中的证书验证

    一.HTTPS的安全性体现在哪 HTTP(超文本传输协议,Hyper Text Transfer Protocol)是我们浏览网站信息传输最广泛的一种协议.HTTPS(Hyper Text Trans ...

  4. extends android.view.ViewGroup两种实现

    /*    private int measureHeight(int heightMeasureSpec) {         int count = getChildCount();        ...

  5. iOS升级swift3 遇到Overriding non-open instance method outside of its defining module的解决方案

    最近将我之前的一个swift项目升级swift3,说多了都是泪... 其中,遇到这样一个错误: 这是用的三方:ENSwiftSideMenu时引出的 报了两个错: 1.Cannot inherit f ...

  6. spring data jpa 小结

    spring data jpa 介绍:  JPA是sun提出的一个对象持久化规范,各JavaEE应用服务器自主选择具体实现,JPA的设计者是Hibernate框架的作者,因此Hibernate作为Jb ...

  7. JMeter学习笔记--JMeter常用测试元件

    JMeter测试计划有一个被称为“函数测试模式”的选项,如果被选择,它会使Jmeter记录来自服务器返回的每个取样的数据.如果你在测试监听器中选择一个文件,这个数据将被写入文件.如果你尝试一个较小的测 ...

  8. Android 在已有的项目上创建新的项目

    原工程 右键Copy   再右键点Paste 改新的工程名

  9. VS报:"dll标记为系统必备组件,必须对其进行强签名"错误

    问题: VS生成程序时,报“要将程序集“XX.dll”标记为系统必备组件,必须对其进行强签名.”错误. 解决方法: 1)在报错的解决方案中找到一个可以发布的项目(引用该XX.dll的项目未必可以发布) ...

  10. Form_如何通过标准功能查找数据源(概念)

    2014-06-01 Created By BaoXinjian