forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
52 lines (43 loc) · 1.13 KB
/
main2.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
49
50
51
52
/// Source : https://leetcode.com/problems/guess-number-higher-or-lower/description/
/// Author : liuyubobobo
/// Time : 2018-04-27
#include <iostream>
#include <cassert>
using namespace std;
// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num);
/// Ternary Search
/// Time Complexity: O(log3(n))
/// Space Complexity: O(1)
class Solution {
public:
int guessNumber(int n) {
int l = 1, r = n;
while(l <= r){
int mid1 = l + (r - l) / 3;
int mid2 = r - (r - l) / 3;
int ret1 = guess(mid1);
if(ret1 == 0)
return mid1;
else if(ret1 < 0)
r = mid1 - 1;
else{
int ret2 = guess(mid2);
if(ret2 == 0)
return mid2;
else if(ret2 < 0){
l = mid1 + 1;
r = mid2 - 1;
}
else
l = mid2 + 1;
}
}
assert(false);
}
};
int main() {
return 0;
}