-
Notifications
You must be signed in to change notification settings - Fork 0
/
findMin.cpp
48 lines (42 loc) · 952 Bytes
/
findMin.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
46
47
48
/* 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.
* You may assume no duplicate exists in the array.
*
*
*/
/*
*
*
*
*/
#include <iostream>
#include <climits>
#include <algorithm>
#include <vector>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <queue>
#include <list>
class Solution {
public:
// O(n)
int findMin0(std::vector<int> &nums) {
return *std::min_element(nums.begin(), nums.end());
}
// O(log n)
int findMin1(std::vector<int> &nums) {
unsigned l = 0, h = nums.size() - 1;
while (l < h) {
auto m = l + (h - l) / 2;
nums[m] > nums[h] ? l = m + 1 : h = m;
}
return nums[l];
}
};
int main() {
std::vector<int> input{4, 5, 6, 7, 1, 2};
std::cout << Solution().findMin1(input) << std::endl;
}