Skip to content

Commit 9febd2e

Browse files
guess number higher or lower
1 parent 7566e15 commit 9febd2e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package easy;
2+
3+
public class GuessNumberHigherorLower {
4+
/**The core problem/trouble to solve this problem is to figure out the problem description:
5+
* this API: guess(int num) means to take your guess num and let you know if your guessed num is bigger or smaller than the answer.
6+
* That's why if num > target, it returns -1 which means the target is smaller than your guess!!!*/
7+
8+
public int guessNumber(int n) {
9+
int left = 1, right = n;
10+
while(left+1 < right){
11+
int mid = left + (right-left)/2;
12+
int g = guess(mid);
13+
if(g == 0) return mid;
14+
else if(g > 0) left = mid;
15+
else right = mid;
16+
}
17+
if(guess(left) == 0) return left;
18+
return right;
19+
}
20+
21+
/**This is a fake guess method that I wrote just to compile/test, I'll have to change it to another number other than 6 based on the number to be found.*/
22+
private int guess(int num) {
23+
if(num > 6){
24+
return -1;
25+
} else if(num < 6){
26+
return 1;
27+
} else {
28+
return 0;
29+
}
30+
}
31+
32+
public static void main(String...strings){
33+
GuessNumberHigherorLower test = new GuessNumberHigherorLower();
34+
System.out.println(test.guessNumber(10));
35+
}
36+
37+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
|388|[Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/)|[Solution](../../blob/master/MEDIUM/src/medium/LongestAbsoluteFilePath.java)| O(n)|O(d) | Medium| Stack
1313
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)|[Solution](../../blob/master/EASY/src/easy/FirstUniqueCharacterinaString.java)| O(n)|O(n) | Easy| HashMap
1414
|386|[Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/)|[Solution](../../blob/master/MEDIUM/src/medium/LexicographicalNumbers.java)| O(n)|O(1) | Medium|
15+
|374|[Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)|[Solution](../../blob/master/EASY/src/easy/GuessNumberHigherorLower.java)| O(logn)|O(1) | Easy| Binary Search
1516
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArraysII.java)| O(m+n)|O((m+n)) could be optimized | Easy| HashMap, Binary Search
1617
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../../blob/master/EASY/src/easy/IntersectionOfTwoArrays.java)| O(m+n)|O(min(m,n)) | Easy| Two Pointers, Binary Search
1718
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)|[Solution] | O(n)|O(n) | Medium| HashMap

0 commit comments

Comments
 (0)