猜數遊戲(求保證能贏的最少錢數)Guess Number Higher or Lower II

問題:less

We are playing the Guess Game. The game is as follows:this

I pick a number from 1 to n. You have to guess which number I picked.spa

Every time you guess wrong, I'll tell you whether the number I picked is higher or lower..net

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.rest

Example:code

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.blog

解決:遊戲

Hint:ip

  1. The best strategy to play the game is to minimize the maximum loss you could possibly face. Another strategy is to minimize the expected loss. Here, we are interested in thefirst scenario.
  2. Take a small example (n = 3). What do you end up paying in the worst case?
  3. Check out this article if you're still stuck.
  4. The purely recursive implementation of minimax would be worthless for even a small n. You MUST use dynamic programming.
  5. As a follow-up, how would you modify your code to solve the problem of minimizing the expected loss, instead of the worst-case loss?

【題意】猜數遊戲,要求你猜錯了就得付與你所猜的數目一致的錢,最後應求出保證你能贏的錢數(即求出保證你獲勝的最少的錢數)get

① 在1-n個數裏面,咱們任意猜一個數(設爲i),保證獲勝所花的錢應該爲 i + max(w(1 ,i-1), w(i+1 ,n)),這裏w(x,y))表示猜範圍在(x,y)的數保證能贏應花的錢,則咱們依次遍歷 1-n做爲猜的數,求出其中的最小值即爲答案,即最小的最大值問題。http://blog.csdn.net/adfsss/article/details/51951658

設dp[i][j]爲當範圍爲(i + 1,j + 1)時,可以保證獲勝的最少錢數。

則有dp[i][j] = k + max(dp[i][k - 1],dp[k + 1][j]);

class Solution { //16ms
    public int getMoneyAmount(int n) {
        int[][] dp = new int[n + 1][n + 1];
        for (int i = 0;i < n + 1;i ++){
            dp[i] = new int[n + 1];
        }
        for (int i = 0;i < n + 1;i ++){//初始化
            for (int j = 0;j < n + 1;j ++){
                dp[i][j] = 0;
            }
        }
        return cost(dp,1,n);
    }
    public int cost(int[][] dp,int i,int j){
        int res = Integer.MAX_VALUE;
        if (i >= j){
            return 0;
        }
        if (dp[i][j] != 0){
            return dp[i][j];
        }
        for (int k = i;k < j + 1;k ++){
            int tmp = k + Math.max(cost(dp,i,k - 1),cost(dp,k + 1,j));
            if (tmp < res){
                res = tmp;
            }
        }
        dp[i][j] = res;
        return res;
    }
}

② 進化版。

class Solution { //4ms     int[][] dp;     public int getMoneyAmount(int n) {         dp = new int[n + 1][n + 1];         return cost(1,n);     }     public int cost(int i,int j){         if (i >= j){             return 0;         }         if (i >= j - 2){             dp[i][j] = j - 1;             return dp[i][j];         }         if (dp[i][j] != 0){             return dp[i][j];         }         int mid = (i + j) / 2 - 1;         int min = Integer.MAX_VALUE;         while(mid < j){             int left = cost(i,mid - 1);             int right = cost(mid + 1,j);             min = Math.min(min,mid + Math.max(left,right));             if (right <= left) break;             mid ++;         }         dp[i][j] = min;         return dp[i][j];     } }

相關文章
相關標籤/搜索