-
Notifications
You must be signed in to change notification settings - Fork 423
/
FindMinimumInRotatedSortedArrayII.py
68 lines (46 loc) · 1.47 KB
/
FindMinimumInRotatedSortedArrayII.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Suppose an array sorted in ascending order 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.
Example 1:
Input: [1,3,5]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0
Note:
This is a follow up problem to Find Minimum in Rotated Sorted Array.
Would allow duplicates affect the run-time complexity? How and why?
与Search in Rotated Sorted Array II 中讨论的一样,主要就是重复的数。用同样的方法即可。
解释请看 SearchInRotatedSortedArrayII.py.
beat 100% 20ms~24ms都有可能。
测试地址:
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/
"""
class Solution(object):
def find_rotate(self, nums):
target = nums[0]
lo = 1
for i in range(1, len(nums)):
if nums[i] == target:
lo += 1
else:
break
hi = len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] > target:
lo = mid + 1
else:
hi = mid
return lo
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
rotate = self.find_rotate(nums)
if rotate == len(nums):
return nums[0]
return nums[rotate]