Java 算法 - Guess Number Higher or Lower II(动态规划和分治法) 题意 样例 补充 1. 解题思路 2. 动态规划 3. 分支法

  今天楼主在LeetCode上面刷到了一道动态规划的题,感觉有必要的记录下来。

题意

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.

样例

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.

1. 解题思路

  初次看到这道题时,我一脸懵逼,后面实在是想不通这道题的意思,参考了大佬的代码并且加以理解。
  我们先来理解一下这道题的意思。题干说给一个数字n,求得最小的支出。我们可以从博弈论的方向来看待这道题,博弈的对方肯定会想法设法让我们最终的结果最大,为什么这么说呢?如果对方不给我们创造阻碍的,那最小值岂不是永远是0(我们选择啥,对方就设置啥)。所以得出如下结论:
  当我们选择m,如果m左边或者右边还有未选择数字,那么博弈方设置的数字肯定是左边和右边的最大结果。抽象成代码的话,假设dp[n + 1][n + 1]数组,其中dp[i][j]表示从i ~ j的最小值,那么我们可以得出动态规划方程:dp[i][j] = Math.min(dp[i][j], select + Math.max(dp[i][select - 1], dp[select + 1][j])),其中select的取值范围是:[i, j]。
  如上分析,我们得出从两种方法来解决这个问题:动态规划和分治法。

2. 动态规划

    public int getMoneyAmount(int n) {
        int dp[][] = new int[n + 1][n + 1];
        for (int right = 1; right <= n; right++) {
            for (int left = right - 1; left >= 1; left--) {
                dp[left][right] = Integer.MAX_VALUE;
                for (int select = right; select >= left; select--) {
                    if (select == left) {
                        dp[left][right] = Math.min(dp[left][right], select + dp[select + 1][right]);
                    } else if (select == right) {
                        dp[left][right] = Math.min(dp[left][right], select + dp[left][right - 1]);
                    } else {
                        dp[left][right] = Math.min(dp[left][right], select + Math.max(dp[left][select - 1], dp[select + 1][right]));
                    }
                }
            }
        }
        return dp[1][n];
    }

3. 分支法

    private int memo[][];

    public int getMoneyAmount(int n) {
        memo = new int[n + 1][n + 1];
        return minCost(1, n);
    }

    private int minCost(int start, int end) {
        if (start >= end) {
            return 0;
        }
        if (memo[start][end] != 0) {
            return memo[start][end];
        }
        int min = Integer.MAX_VALUE;
        for (int i = start; i <= end; i++) {
            min = Math.min(min, i + Math.max(minCost(start, i - 1), minCost(i + 1, end)));
        }
        memo[start][end] = min;
        return min;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章