leet-code/src/main/java/leetcode/editor/cn/GuessNumberHigherOrLowerIi.java
huangge1199@hotmail.com 8cfa166dea 375:猜数字大小 II
2021-11-12 13:39:55 +08:00

97 lines
4.2 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//我们正在玩一个猜数游戏,游戏规则如下:
//
//
// 我从 1 到 n 之间选择一个数字。
// 你来猜我选了哪个数字。
// 如果你猜到正确的数字,就会 赢得游戏 。
// 如果你猜错了,那么我会告诉你,我选的数字比你的 更大或者更小 ,并且你需要继续猜数。
// 每当你猜了数字 x 并且猜错了的时候,你需要支付金额为 x 的现金。如果你花光了钱,就会 输掉游戏 。
//
//
// 给你一个特定的数字 n ,返回能够 确保你获胜 的最小现金数,不管我选择那个数字 。
//
//
//
// 示例 1
//
//
//输入n = 10
//输出16
//解释:制胜策略如下:
//- 数字范围是 [1,10] 。你先猜测数字为 7 。
//  - 如果这是我选中的数字,你的总费用为 $0 。否则,你需要支付 $7 。
//  - 如果我的数字更大,则下一步需要猜测的数字范围是 [8,10] 。你可以猜测数字为 9 。
//  - 如果这是我选中的数字,你的总费用为 $7 。否则,你需要支付 $9 。
//  - 如果我的数字更大,那么这个数字一定是 10 。你猜测数字为 10 并赢得游戏,总费用为 $7 + $9 = $16 。
//  - 如果我的数字更小,那么这个数字一定是 8 。你猜测数字为 8 并赢得游戏,总费用为 $7 + $9 = $16 。
//  - 如果我的数字更小,则下一步需要猜测的数字范围是 [1,6] 。你可以猜测数字为 3 。
//  - 如果这是我选中的数字,你的总费用为 $7 。否则,你需要支付 $3 。
//  - 如果我的数字更大,则下一步需要猜测的数字范围是 [4,6] 。你可以猜测数字为 5 。
//  - 如果这是我选中的数字,你的总费用为 $7 + $3 = $10 。否则,你需要支付 $5 。
//  - 如果我的数字更大,那么这个数字一定是 6 。你猜测数字为 6 并赢得游戏,总费用为 $7 + $3 + $5 = $15 。
//  - 如果我的数字更小,那么这个数字一定是 4 。你猜测数字为 4 并赢得游戏,总费用为 $7 + $3 + $5 = $15 。
//  - 如果我的数字更小,则下一步需要猜测的数字范围是 [1,2] 。你可以猜测数字为 1 。
//  - 如果这是我选中的数字,你的总费用为 $7 + $3 = $10 。否则,你需要支付 $1 。
//  - 如果我的数字更大,那么这个数字一定是 2 。你猜测数字为 2 并赢得游戏,总费用为 $7 + $3 + $1 = $11 。
//在最糟糕的情况下,你需要支付 $16 。因此,你只需要 $16 就可以确保自己赢得游戏。
//
//
// 示例 2
//
//
//输入n = 1
//输出0
//解释:只有一个可能的数字,所以你可以直接猜 1 并赢得游戏,无需支付任何费用。
//
//
// 示例 3
//
//
//输入n = 2
//输出1
//解释:有两个可能的数字 1 和 2 。
//- 你可以先猜 1 。
//  - 如果这是我选中的数字,你的总费用为 $0 。否则,你需要支付 $1 。
//  - 如果我的数字更大,那么这个数字一定是 2 。你猜测数字为 2 并赢得游戏,总费用为 $1 。
//最糟糕的情况下,你需要支付 $1 。
//
//
//
//
// 提示:
//
//
// 1 <= n <= 200
//
// Related Topics 数学 动态规划 博弈 👍 354 👎 0
package leetcode.editor.cn;
//375:猜数字大小 II
class GuessNumberHigherOrLowerIi {
public static void main(String[] args) {
//测试代码
Solution solution = new GuessNumberHigherOrLowerIi().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int getMoneyAmount(int n) {
int[][] f = new int[n + 1][n + 1];
for (int i = n - 1; i >= 1; i--) {
for (int j = i + 1; j <= n; j++) {
int minCost = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
int cost = k + Math.max(f[i][k - 1], f[k + 1][j]);
minCost = Math.min(minCost, cost);
}
f[i][j] = minCost;
}
}
return f[1][n];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}