374. Guess Number Higher or Lower
Problem Description
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
You call a pre-defined API int guess(int num), which returns three possible results:
-1: Your guess is higher than the number I picked (num > pick)1: Your guess is lower than the number I picked (num < pick)0: your guess is equal to the number I picked (num == pick)
Return the number that I picked.
Example 1:
Input: n = 10, pick = 6
Output: 6
Example 2:
Input: n = 1, pick = 1
Output: 1
Example 3:
Input: n = 2, pick = 1
Output: 1
Constraints:
1 <= n <= 2³¹ - 11 <= pick <= n
Solution
Python Solution
# The guess API is already defined for you.
# @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
# def guess(num: int) -> int:
class Solution:
def guessNumber(self, n: int) -> int:
left, right = 1, n
while left <= right:
mid = left + (right - left) // 2
result = guess(mid)
if result == 0:
return mid
elif result == 1:
left = mid + 1
else:
right = mid - 1
return left # This line should never be reached
Time Complexity: O(log n)
Binary search reduces the search space by half in each iteration.
Space Complexity: O(1)
Only uses a constant amount of extra space.
Java Solution
/**
* 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;
int right = n;
while (left <= right) {
int mid = left + (right - left) / 2;
int result = guess(mid);
if (result == 0) {
return mid;
} else if (result == 1) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left; // This line should never be reached
}
}
Time Complexity: O(log n)
Binary search reduces the search space by half in each iteration.
Space Complexity: O(1)
Only uses a constant amount of extra space.
C++ Solution
/**
* 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);
*/
class Solution {
public:
int guessNumber(int n) {
int left = 1;
int right = n;
while (left <= right) {
int mid = left + (right - left) / 2;
int result = guess(mid);
if (result == 0) {
return mid;
} else if (result == 1) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left; // This line should never be reached
}
};
Time Complexity: O(log n)
Binary search reduces the search space by half in each iteration.
Space Complexity: O(1)
Only uses a constant amount of extra space.
JavaScript Solution
/**
* Forward declaration of guess API.
* @param {number} 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
* var guess = function(num) {}
*/
/**
* @param {number} n
* @return {number}
*/
var guessNumber = function(n) {
let left = 1;
let right = n;
while (left <= right) {
const mid = left + Math.floor((right - left) / 2);
const result = guess(mid);
if (result === 0) {
return mid;
} else if (result === 1) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left; // This line should never be reached
};
Time Complexity: O(log n)
Binary search reduces the search space by half in each iteration.
Space Complexity: O(1)
Only uses a constant amount of extra space.
C# Solution
/**
* 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 : GuessGame {
public int GuessNumber(int n) {
int left = 1;
int right = n;
while (left <= right) {
int mid = left + (right - left) / 2;
int result = guess(mid);
if (result == 0) {
return mid;
} else if (result == 1) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left; // This line should never be reached
}
}
Time Complexity: O(log n)
Binary search reduces the search space by half in each iteration.
Space Complexity: O(1)
Only uses a constant amount of extra space.
Approach Explanation
The solution uses binary search to efficiently find the picked number:
- Key Insights:
- Binary search is optimal for guessing numbers
- The search space is sorted (1 to n)
- The API provides direction for search
- Each guess eliminates half the possibilities
- Algorithm Steps:
- Initialize left and right boundaries
- Calculate middle point
- Use guess API to get feedback
- Adjust search space based on feedback
Implementation Details:
- Safe middle calculation to avoid overflow
- Proper boundary updates
- Early termination on correct guess
- Efficient search space reduction
Optimization Insights:
- No need for extra space
- Logarithmic time complexity
- Minimal API calls
- Efficient boundary handling
Edge Cases:
- n = 1 (single number)
- Pick at boundaries
- Large values of n
- Integer overflow prevention