-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe374.java
33 lines (27 loc) · 812 Bytes
/
e374.java
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
// This is just bin search but with casting to long cause of int overflow lol
/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is higher than the picked number
* 1 if num is lower than the picked number
* otherwise return 0
* int guess(int num);
*/
public class Solution extends GuessGame {
public int guessNumber(int n) {
int left = 1, right = n;
while (left < right) {
int mid = (int) (((long) left + (long) right) / 2);
int gs = guess(mid);
if (gs == 0) {
return mid;
}
if (gs == -1) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
}