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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章