375. Guess Number Higher or Lower II
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum amount of money required to guarantee a win in a number guessing game. We are given a range of numbers from 1 to n, and we need to devise a guessing strategy. For each wrong guess x, we pay x dollars and are told whether the secret number is higher or lower. This continues until we guess correctly.
Solution
Guess Number Higher or Lower II in Python
class Solution:
def getMoneyAmount(self, n: int) -> int:
# dp[i][j] stores the minimum cost to guarantee a win in the range [i, j].
# We use a (n+2)x(n+2) table for 1-based indexing from 1 to n, which matches
# the problem statement. dp[i][i-1] will be 0, representing an empty range.
dp = [[0] * (n + 2) for _ in range(n + 2)]
# length is the length of the range [i, j]
for length in range(2, n + 1):
# i is the starting point of the range
for i in range(1, n - length + 2):
j = i + length - 1
dp[i][j] = float('inf')
# k is the number we guess within the range [i, j]
for k in range(i, j + 1):
# If we guess k and it's wrong, we pay k dollars.
# The actual number is either in [i, k-1] or [k+1, j].
# We need to prepare for the worst case, which costs max(dp[i][k-1], dp[k+1][j]).
cost = k + max(dp[i][k - 1], dp[k + 1][j])
dp[i][j] = min(dp[i][j], cost)
return dp[1][n]
Guess Number Higher or Lower II in Java
class Solution {
public int getMoneyAmount(int n) {
// dp[i][j] stores the minimum cost to guarantee a win in the range [i, j].
// Using n+2 size to handle boundary cases gracefully. For example, when k=i, dp[i][k-1]
// becomes dp[i][i-1], which is 0 (cost for an empty range).
int[][] dp = new int[n + 2][n + 2];
// len is the length of the range [i, j]
for (int len = 2; len <= n; len++) {
// i is the starting point of the range
for (int i = 1; i <= n - len + 1; i++) {
int j = i + len - 1;
dp[i][j] = Integer.MAX_VALUE;
// k is the number we guess within the range [i, j]
for (int k = i; k <= j; k++) {
// If we guess k and it's wrong, we pay k dollars.
// The actual number is either in [i, k-1] or [k+1, j].
// We need to prepare for the worst case, which costs max(dp[i][k-1], dp[k+1][j]).
int cost = k + Math.max(dp[i][k - 1], dp[k + 1][j]);
dp[i][j] = Math.min(dp[i][j], cost);
}
}
}
return dp[1][n];
}
}
Guess Number Higher or Lower II in C++
class Solution {
public:
int getMoneyAmount(int n) {
// dp[i][j] stores the minimum cost to guarantee a win in the range [i, j].
// Using n+2 size to handle boundary cases gracefully. For example, when k=i, dp[i][k-1]
// becomes dp[i][i-1], which is 0 (cost for an empty range).
vector<vector<int>> dp(n + 2, vector<int>(n + 2, 0));
// len is the length of the range [i, j]
for (int len = 2; len <= n; ++len) {
// i is the starting point of the range
for (int i = 1; i <= n - len + 1; ++i) {
int j = i + len - 1;
dp[i][j] = INT_MAX;
// k is the number we guess within the range [i, j]
for (int k = i; k <= j; ++k) {
// If we guess k and it's wrong, we pay k dollars.
// The actual number is either in [i, k-1] or [k+1, j].
// We need to prepare for the worst case, which costs max(dp[i][k-1], dp[k+1][j]).
int cost = k + max(dp[i][k - 1], dp[k + 1][j]);
dp[i][j] = min(dp[i][j], cost);
}
}
}
return dp[1][n];
}
};
Guess Number Higher or Lower II in TypeScript
function getMoneyAmount(n: number): number {
// dp[i][j] stores the minimum cost to guarantee a win in the range [i, j].
// Using n+2 size to handle boundary cases gracefully. For example, when k=i, dp[i][k-1]
// becomes dp[i][i-1], which is 0 (cost for an empty range).
const dp: number[][] = Array(n + 2).fill(0).map(() => Array(n + 2).fill(0));
// len is the length of the range [i, j]
for (let len = 2; len <= n; len++) {
// i is the starting point of the range
for (let i = 1; i <= n - len + 1; i++) {
const j = i + len - 1;
dp[i][j] = Number.MAX_SAFE_INTEGER;
// k is the number we guess within the range [i, j]
for (let k = i; k <= j; k++) {
// If we guess k and it's wrong, we pay k dollars.
// The actual number is either in [i, k-1] or [k+1, j].
// We need to prepare for the worst case, which costs max(dp[i][k-1], dp[k+1][j]).
const cost = k + Math.max(dp[i][k - 1], dp[k + 1][j]);
dp[i][j] = Math.min(dp[i][j], cost);
}
}
}
return dp[1][n];
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.