The question:

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.

My first ugly solution:

public class Solution {
public int findMin(int[] num) {
if (num == null || num.length == 0)
return -1; int low = 0;
int high = num.length - 1;
int mid = -1; while (low < high) {
mid = (low + high) / 2;
if (num[low] == num[mid] && low != mid) {
low ++;
continue;
}
if (num[low] <= num[mid] && num[low] >= num[high])
low = mid + 1;
else
high = mid;
}
return num[low];
}
}

An improved analysis and solution.

This problem involves a very imporant skills in binary searching. It's very very tricky!!!
The idea behind this problem is not complex. It could be classified into following three cases:
1.A[mid] > A[low], we could discard this part, and search in the range[mid+1, high]
2.A[mid] < A[low], the mininum must be included in the first part range[low, mid]. Note the mid might be the answer, thus we could not discard it.
3.A[mid] == A[low]. In this case we could not distinguish which part may hold the anwser, but we need to proceed on. One solution is to move low pointer one step forward. We could discard A[low]. The resulting answer is:
public int findMin(int[] num) {
if (num == null || num.length == 0)
return -1;
int low = 0;
int high = num.length - 1;
int mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (num[low] > num[mid])
high = mid;
else if (num[low] < num[mid])
low = mid + 1;
else
low ++;
}
return num[low];
}
However this solution has a very big pitfall!!!
Because the "divide operation" only return the interger part "mid = (low + high) / 2;", Before exiting from the loop, the low pointer could point to the same element with mid pointer. Consider following two cases:
1. 3 1
low : num[0] = 3, mid: num[0] = 3.
result in low++; return answer num[1] = 1. (The right answer) 2. 1 3
low : num[0] = 3, mid: num[0] = 3.
result in low++; return answer num[1] = 3. (The wrong answer) What a big problem!!!
An very elegant way to solve this problem. Using the same idea, we compare num[mid] and num[high], then update the solution into: while (low < high) {
mid = (low + high) / 2; if (num[mid] > num[high])
low = mid + 1;
else if (num[low] < num[mid])
high = mid;
else
high--;
}
return[low]; //note we reuturn[low] at here!!! If this fix works?
1. 3 1
high : num[0] = 1, mid: num[0] = 3.
result in high--; return answer num[0] = 1. (The right answer) 2. 1 3
high : num[1] = 3, mid: num[0] = 1.
result in high = mid = 0; return answer num[0] = 1. (The right answer) The reason is that: the high pointer would not point to the same element with mid. The invariant holds to the end!!!
iff high == mid, high must equal to low, since while (low < high), this case is impossible. Note:
The idea behind this method is that, this method could properly tackle the case when there are only two elements left. When only two elements left, the low pointer and mid pointer point to the same element. By comparing with the mid pointer, we could guarantee to reach the right answer by returing num[low].
if (num[mid] > num[high])
low = mid + 1;
else if (num[low] < num[mid])
high = mid;
else
high--;
When num[mid](low) > num[high], we move the low pointer to high. (low++), A[low] is the smaller.
When num[mid](low) < num[high], we keep low in the same position, A[low] is the smaller.

The improved solution:

public class Solution {
public int findMin(int[] num) {
if (num == null || num.length == 0)
return -1; int low = 0;
int high = num.length - 1;
int mid = -1; while (low < high) {
mid = (low + high) / 2; if (num[mid] > num[high])
low = mid + 1;
else if (num[low] < num[mid])
high = mid;
else
high--;
}
return num[low];
}
}

[LeetCode#154]Find Minimum in Rotated Sorted Array II的更多相关文章

  1. [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  2. [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

      Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i. ...

  3. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. leetcode 154. Find Minimum in Rotated Sorted Array II --------- java

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  5. LeetCode 154. Find Minimum in Rotated Sorted Array II寻找旋转排序数组中的最小值 II (C++)

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

  6. LeetCode 154.Find Minimum in Rotated Sorted Array II(H)(P)

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

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

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

  8. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

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

随机推荐

  1. Java语言基础(七)

    Java语言基础(七) 今天在公司多呆了会,回来晚了 一.自动类型转换 在Java中,数据是可以转换的  例如可以将byte类型的赋给int类型的 这里希望你了解内存的变化,例如 在这里,我想你应该知 ...

  2. 关于ADO.NET的一些知识整理

    ADO.NET是什么 虽然我们都知道ADO.NET是对数据库的操作,但是要真的说出ADO.NET的具体含义还不是很容易. ADO.NET是ActiveX Data Objects的缩写,它是一个COM ...

  3. 查看sql语句执行时间/测试sql语句性能

    写程序的人,往往需要分析所写的SQL语句是否已经优化过了,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了. 通过设置STATISTICS我们可以查看执行SQL时的 ...

  4. JavaScript学习笔记--ES6学习(五) 数值的扩展

    ES6 对于数值类型 (Number) 进行了一下扩展: 1.对于二进制和八进制提供了新的写法 ES6对于二进制和八进制的数值提供了新的写法,分别用0b (或者0B) 和0o (或者0o) 表示.例如 ...

  5. IE6解决固定定位代码

    有些朋友在进行网页布局时,会遇到IE6浏览器不支持固定定位的兼容性问题,本博将详细介绍此问题的解决方法,需要了解的朋友可以参考下. ie6 垂直居中固定定位,代码如下: #center {_posit ...

  6. HttpClient的get+post请求使用

    啥都不说,先上代码 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReade ...

  7. IE6中的常见BUG与相应的解决办法

    开发前端的同学一定都知道,IE6是兼容BUG最多的浏览器,它不支持PNG alpha通道暂且不论.其文档的解析理解规范也引起了诸多恼人的BUG,有时甚至让人感到绝望.本文主要讲解一些比较容易遇到的IE ...

  8. Devexpress Barmanager设置

    一,在bar的属性中有optionbar,可以做一些设置. 其中比较有用的是:1,去掉最右边的箭头:allowquickcustomization 改为false 2,去掉最左边的竖线:drawdra ...

  9. 动态改变数据库连接 in Entity Framework 5

    今天把silverlight 升级到5,ADO.ENT EF也用NUGet升级到5.结果发现5下的EF默认没有4的那种分部方法了. 当然你可以把生成器的属性里面,生成代码的属性替换为default,默 ...

  10. 分页插件jquery.simplePagination.js使用

    利用ecshop后台,利用插件更改分页显示样式遇到的问题,由于是利用Ajax获取数据进行页面数据更新?所以出现了以下情况: 初始化页面前 : 分页更新后: 点击后出现了分页插件内容消失, 原因:分页一 ...