Skip to content

Commit 32c3df0

Browse files
committed
🚀 26-Jul-2020
1 parent f4a686e commit 32c3df0

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
2+
3+
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
4+
5+
Find the minimum element.
6+
7+
The array may contain duplicates.
8+
9+
Example 1:
10+
11+
Input: [1,3,5]
12+
Output: 1
13+
Example 2:
14+
15+
Input: [2,2,2,0,1]
16+
Output: 0
17+
Note:
18+
19+
This is a follow up problem to Find Minimum in Rotated Sorted Array.
20+
Would allow duplicates affect the run-time complexity? How and why?
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
class Solution {
31+
public:
32+
int findMin(vector<int>& nums) {
33+
int l = 0,r = nums.size() - 1,mid = 0;
34+
while(l < r) {
35+
mid = l + (r - l) / 2;
36+
if (nums[mid] > nums[r]) l = mid + 1;
37+
else if (nums[mid] < nums[r]) r = mid;
38+
else r--;
39+
}
40+
return nums[l];
41+
}
42+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
2+
3+
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
4+
5+
Find the minimum element.
6+
7+
The array may contain duplicates.
8+
9+
Example 1:
10+
11+
Input: [1,3,5]
12+
Output: 1
13+
Example 2:
14+
15+
Input: [2,2,2,0,1]
16+
Output: 0
17+
Note:
18+
19+
This is a follow up problem to Find Minimum in Rotated Sorted Array.
20+
Would allow duplicates affect the run-time complexity? How and why?
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
class Solution {
31+
public:
32+
int findMin(vector<int>& nums) {
33+
int l = 0,r = nums.size() - 1,mid = 0;
34+
while(l < r) {
35+
mid = l + (r - l) / 2;
36+
if (nums[mid] > nums[r]) l = mid + 1;
37+
else if (nums[mid] < nums[r]) r = mid;
38+
else r--;
39+
}
40+
return nums[l];
41+
}
42+
};

0 commit comments

Comments
 (0)