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'll tell you whether the number I picked is higher or lower.
However, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked.
Example:
n = 10, I pick 8.
First round: You guess 5, I tell you that it's higher. You pay $5.
Second round: You guess 7, I tell you that it's higher. You pay $7.
Third round: You guess 9, I tell you that it's lower. You pay $9.
Game over. 8 is the number I picked.
You end up paying $5 + $7 + $9 = $21.
Given a particular n ≥ 1, find out how much money you need to have to guarantee a win.
这题之前有一个374题,给了一个predefined函数,guess,根据你猜的数字返回大小关系,我们需要利用这个函数返回正确值,这就是一个典型的二分法啦,binary search,十分简单,所以不再在博客里赘述。这题的话就比较有讨论的意义,这是一个典型的minimax的问题,可以参考wikipedia以及很多的算法博客,此处不做展开。
这题基本都是要用dynamic programming实现的,建立一个dp[n+1][n+1],dp[i][j]表示正确答案在i到j之间的时候,如果要确保找出答案需要付出的最小的代价。这种时候,我们可以讨论第一次猜k, k在i,j之间,这个时候确保猜对需要k+max(dp[i][k-1], dp[k+1][j]),这也就是我们的state equation。当我们循环试探每一个在i,j区间的k的时候,我们能知道每一个k的需要确保找到答案的代价,找出这些代价里最小的,就是我们的dp[i][j]。
对照wikipedia上的这段话理解下:
The minimax value of a player is the smallest value that the other players can force the player to receive, without knowing his actions. Equivalently, it is the largest value the player can be sure to get when he knows the actions of the other players. Its formal definition is:
![]()
具体实现的时候,可以递归实现,也可以循环实现(bottom-up)。
方法一:递归实现dp
int getMoneyAmount(int n) {
vector<vector<int>> dp(n+1, vector<int>(n+1, 0));
return DP(dp, 1, n);
}
int DP (vector<vector<int>> &dp, int s, int e) {
if (s >= e) return 0;
if (dp[s][e] > 0) return dp[s][e];
int res = INT_MAX;
for (int i = s; i <= e; i++) {
int temp = i + max(DP(dp, s, i-1), DP(dp, i+1, e));
res = min(res, temp);
}
return dp[s][e] = res;
}
方法二:bottom-up
int getMoneyAmount(int n) {
vector<vector<int>> dp(n+1, vector<int>(n+1, 0));
//dp[i][j],if i >= j, which should be 0, so you don't need to handle with them in following
for (int i = 2; i <= n; i++) {
for (int j = i-1; j >= 1; j--) {
int res = INT_MAX;
for (int k = j; k < i; k++) {
int temp = k + max(dp[j][k-1], dp[k+1][i]);
res = min(res, temp);
}
dp[j][i] = res;
}
}
return dp[1][n];
}