https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode

follow up question: find the minimum element in a rotated array with duplicate elements

Idea: [3,3,1,3] compared to [3,4,1,2]  -> l = mid+1 --1

[1,3,3] compared to [1,3,4] - > r = mid;  -- 2

If we used the previous solution, it will complain with the above cases because one case is : nums[mid] > nums[r] l = mid+1; violating the 2nd case to update r ot l

So here is the hadful problem.

At first, I am trying to seperate this case with one conditon:  nums[mid] == nums[r] or..... However that is not general

Totally, (nums[l] == nums[mid]) && (nums[r] == nums[mid]) {left++, right--} skip相同的元素。知道不同为止。

Dealing with special case [1,1] or [1] -- determine the case of left and right.

Idea is from the https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/ -- search in rotated array.

public int findMin(int[] nums) {
int l =0, r = nums.length-1;
while(l<r){
int mid = l+(r-l)/2;
while((nums[l] == nums[mid]) && (nums[r] == nums[mid]) ) {
++l; --r;
if(l>=nums.length || r<0) break;
}
if(l==r) return nums[l];
else if(l>r) return nums[r+1];
if(nums[mid] > nums[r]) l = mid+1;
else r = mid;
}
return nums[l];
}

follow up: find the minimum value index

Other thoughts for this problem: if(nums[mid] == nums[r]) h--; /./skip the current high one..

好难得二分查找。。。。

to be continued...

154. Find Minimum in Rotated Sorted Array II(Binary search)的更多相关文章

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

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

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

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

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

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

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

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

  8. [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 ...

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

随机推荐

  1. day_05 字典

    1. 字典 1.成对保存数据 ,以key:value形式保存 2.以{}表示,每项内容都是key:value,元素之间用逗号隔开 3.key是不可重复的 4.字典以hash算法来计算key的hash值 ...

  2. storm定时器

    package com.example.mail; import org.apache.storm.Config; import org.apache.storm.LocalCluster; impo ...

  3. unittest框架

    在我们真正的编写测试用例之前,我们需要了解一下测试框架. unittest是python自带的单元测试框架,尽管其主要是为单元测试服务的,但我们也可以用它来做接口的自动化测试. unittest框架为 ...

  4. How to Setup MySQL (Master-Slave) Replication in CentOS

    The following tutorial aims to provide you a simple step-by-step guide for setting up MySQL (Master- ...

  5. Ubuntu 安装 phpredis扩展

    官网 https://github.com/phpredis/phpredis 下载->然后解压->上传服务器 /etc/phpredis 进行 cd /etc/phpredisphpiz ...

  6. Photoshop入门教程(二):暂存盘设置与标尺设置

    新建文档之后大家就可以对图像进行编辑.在对图像进行编辑之前,先来了解一下如何查看图像的一些基本信息.在软件左下角,会有这样的信息显示窗口. 1窗口表示当前图像显示比例,200%代表当前为放大两倍显示. ...

  7. mapreduce去重

    现有一个某电商网站的数据文件,名为buyer_favorite1,记录了用户收藏的商品以及收藏的日期,文件buyer_favorite1中包含(用户id,商品id,收藏日期)三个字段,数据内容以“\t ...

  8. 在Mac上安装mysql

    进入这个网站: https://dev.mysql.com/downloads/mysql/ 然后点击安装就行了. 注意在这里启动mysql 然后mac上所有的mysql命令都得用绝对路径才能生效

  9. OCR 维护 crsd.log

    ###########sample 1 OCR corruption messages are reported in crsd.log, automatic OCR backup is failin ...

  10. Index Skip Scan in Oracle in 11g

    http://viralpatel.net/blogs/oracle-index-skip-scan/ in 11g the same sql use index skip scan but in 1 ...