原题
思路:
miniMax+DP
dp[i][j]保存在i到j范围内,猜中这个数字需要花费的最少 money。
“至少需要的花费”,就要我们 “做最坏的打算,尽最大的努力”,即取最大值。
dp[beg][end] =MIN ( i + max(helper(beg, i - 1, dp), helper(i + 1, end, dp)) )
class Solution
{
public:int helper(int beg, int end, vector<vector<int>> &dp){if (beg >= end){return 0;}if (dp[beg][end] != 0){return dp[beg][end];}int minPrice = 99999;int temp = 0;for (int i = beg; i <= end; i++){temp = i + max(helper(beg, i - 1, dp), helper(i + 1, end, dp));if (temp < minPrice){minPrice = temp;}}return dp[beg][end] = minPrice;}int getMoneyAmount(int n){vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));return helper(1, n, dp);}
};