-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path153.cpp
45 lines (38 loc) · 1007 Bytes
/
153.cpp
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
// 153. Find Minimum in Rotated Sorted Array - https://leetcode.com/problems/find-minimum-in-rotated-sorted-array
#include "bits/stdc++.h"
using namespace std;
class Solution {
public:
int bin_search_pivot() {
int L = 0 - 1;
int R = (n - 1) + 1;
int last_element = nums_[n - 1];
// 0000111
auto check = [&last_element](int middle_element) -> bool {
return middle_element <= last_element;
};
while (R - L > 1) {
int M = L + (R - L) / 2;
if (check(nums_[M])) {
R = M;
} else {
L = M;
}
}
return R;
}
int findMin(vector<int>& nums) {
if (nums.empty()) { return -1; }
nums_ = nums;
n = (int)nums_.size();
int pivot = bin_search_pivot();
return nums_[pivot];
}
private:
vector<int> nums_;
int n;
};
int main() {
ios::sync_with_stdio(false);
return 0;
}