-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5
32 lines (28 loc) · 979 Bytes
/
day5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Question:
05. Find Minimum in Rotated Sorted Array
Description
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.
You may assume no duplicate exists in the array.
Example
Given [4, 5, 6, 7, 0, 1, 2] return 0
oj:https://www.lintcode.com/problem/find-minimum-in-rotated-sorted-array/description
-------------------------------------------------------------------------------------------------
answer:
class Solution:
"""
@param nums: a rotated sorted array
@return: the minimum number in the array
"""
def findMin(self, nums):
if not nums:
return -1
start, end = 0, len(nums) - 1
while start + 1 < end:
mid = (start + end) // 2
if nums[mid] > nums[end]:
start = mid
else:
end = mid
return min(nums[start], nums[end])