-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessNumber.java
More file actions
49 lines (37 loc) · 972 Bytes
/
GuessNumber.java
File metadata and controls
49 lines (37 loc) · 972 Bytes
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
package com.vinay.practice.lc;
/**
* 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);
*/
// https://leetcode.com/problems/guess-number-higher-or-lower/submissions/
/*
class GuessNumber {
public int guessNumber(int n) {
//O(n) starts
for(int i=0; i<n; i++){
if(guess(i) == 0){
return i;
}
}
return n;
// O(n) ends
int start = 0;
int end = n-1;
while(start <= end){
int mid = start + (end-start)/2;
if(guess(mid) == -1){
end = mid - 1;
} else if(guess(mid) == 1) {
start = mid+1;
} else{
return mid;
}
}
return n;
}
}
*/