[Leetcode]153. Find Minimum in Rotated Sorted Array
来源:互联网 发布:c语言char函数定义 编辑:程序博客网 时间:2024/05/16 13:45
Obviously this question can be solved within O(n)
time by traversing the whole array and looking for the position i
that satisfies nums[i] < nums[i - 1]
.
However, we haven’t make use of the sorted feature of the array yet.
By take sorted into consideration, we can use a slightly changed binary search way and reduce the runtime to O(logn)
.
Noticing that the input array is sorted in ascending order and is rotated.
- Analyis:
- The input array is sorted in ascending order and is rotated, this leads to 2 possible conditions:
- The input array is rotated right k positions where
k % nums.length = 0
. Under this condition, the input array is actually equal to an unroated one. Thus the minimum is the simply the first element. - The input array is rotated right k positions where
k % nums.length != 0
. Under this conditoion, the input array need to be analysed as following step. - Under the second condition, the input array is actually sepearted to 2 parts, the minimum element is at position k. Then the 2 parts is
0 - (k - 1)
andk to (n - 1)
, both in ascending order and satifies equationnums[0] > nums[nums.length]
andnums[i] > nums[j]
wherei
belongs to0 to k - 1
andj
belongs tok to n - 1
. - Thus the algorithm can be described as following.
- If the array is not rotated or has a length of 1, the first element is the minimum. Otherwise, do following steps.
- Set
start = 0
,end = nums.length - 1
. - Use binary search.
mid = start + (end - start) / 2
. - If mid is located on the left part of the graph, indicating by
nums[mid] > nums[start]
, setstart = mid
. - If mid is located on the right part of the graph, indicating by
nums[mid] < nums[end]
, setend = mid
. - if
nums[mid] < nums[mid - 1]
, thennums[mid]
is the minimum. - The stop condition of the binary search is
start < end - 1
since we are using left skewing binary search (i.e.(start + end) / 2
is always floored to left side). Once thestart
reaches the rightmost position of left part andend
reaches the leftmost position of the right part,nums[end]
ornums[start + 1]
is the minimum. Thus the loop should be terminated.
O(1)
space and O(logn)
time. n
is the length of the input array.
The algorithm implementation using Java is showed as following.
public class Solution { public int findMin(int[] nums) { if (nums == null || nums.length == 0) return 0; int start = 0; int end = nums.length - 1; if (nums[start] <= nums[end]) { //the array is not rotated, the first element is the smallest return nums[start]; } while (start < end - 1) { int mid = start + (end - start) / 2; if (mid > 0 && nums[mid] < nums[mid - 1]) { return nums[mid]; } if (nums[mid] >= nums[start]) { start = mid; } if (nums[mid] <= nums[end]) { end = mid; } } return nums[start + 1]; }}
0 0
- Leetcode 153. Find Minimum in Rotated Sorted Array & Leetcode 154. Find Minimum in Rotated Sorted Ar
- 153. Find Minimum in Rotated Sorted Array--Array--LeetCode--C++
- [LeetCode] Find Minimum in Rotated Sorted Array
- Leetcode: Find Minimum in Rotated Sorted Array
- leetcode :Find Minimum in Rotated Sorted Array
- LeetCode:Find Minimum in Rotated Sorted Array
- 【Leetcode】Find Minimum in Rotated Sorted Array
- LeetCode:Find Minimum in Rotated Sorted Array
- LeetCode: Find Minimum in Rotated Sorted Array
- leetcode: Find Minimum in Rotated Sorted Array
- [LeetCode] Find Minimum in Rotated Sorted Array
- Find Minimum in Rotated Sorted Array -- LeetCode
- Leetcode: Find Minimum in Rotated Sorted Array
- leetcode Find Minimum in Rotated Sorted Array
- leetcode - Find Minimum in Rotated Sorted Array
- LeetCode Find Minimum in Rotated Sorted Array
- Leetcode--Find Minimum in Rotated Sorted Array
- leetcode: Find Minimum in Rotated Sorted Array
- 动态链接库(DLL)
- LeetCode 58. Length of Last Word
- Space Shooter项目中遇到的问题
- python day1(20170220)
- 【解决问题】windows下配置Boost提示'cl' 不是内部或外部命令,也不是可运行的程序 或批处理文件
- [Leetcode]153. Find Minimum in Rotated Sorted Array
- Sublime Text自定制代码片段(Code Snippets)
- 转换后缓冲器
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E
- 2.20.1 绝对值排序
- 欢迎使用CSDN-markdown编辑器
- 简易网页浏览器
- windows 安装aws cli
- 文章标题