Stone Game V

There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.

In each round of the game, Alice divides the row into two non-empty rows (i.e. left row and right row), then Bob calculates the value of each row which is the sum of the values of all the stones in this row. Bob throws away the row which has the maximum value, and Alice's score increases by the value of the remaining row. If the value of the two rows are equal, Bob lets Alice decide which row will be thrown away. The next round starts with the remaining row.

The game ends when there is only one stone remaining. Alice's is initially zero.

Return the maximum score that Alice can obtain.

Example 1:

Input: stoneValue = [6,2,3,4,5,5]
Output: 18
Explanation: In the first round, Alice divides the row to [6,2,3], [4,5,5]. The left row has the value 11 and the right row has value 14. Bob throws away the right row and Alice's score is now 11.
In the second round Alice divides the row to [6], [2,3]. This time Bob throws away the left row and Alice's score becomes 16 (11 + 5).
The last round Alice has only one choice to divide the row which is [2], [3]. Bob throws away the right row and Alice's score is now 18 (16 + 2). The game ends because only one stone is remaining in the row.

思路:DFS + Cache. 比赛写出来很开心;

class Solution {
    public int stoneGameV(int[] A) {
        int n = A.length;
        int[] prefix = new int[n + 1];
        prefix[0] = 0;
        for(int i = 1; i <= n; i++) {
            prefix[i] = prefix[i - 1] + A[i - 1];
        }
        int[][] cache = new int[n][n];
        return dfs(A, 0, n - 1, cache, prefix);
    }
    
    private int dfs(int[] A, int start, int end, int[][] cache, int[] prefix) {
        if(start >= end) {
            return 0;
        }
        if(cache[start][end] != 0) {
            return cache[start][end];
        }
        int localmax = 0;
        for(int k = start; k < end; k++) {
            int localvalue = 0;
            int leftsum = getSum(prefix, start, k); 
            int rightsum = getSum(prefix, k + 1, end); 
            
            if(leftsum > rightsum) {
                localvalue = rightsum + dfs(A, k + 1, end, cache, prefix);
            } else if(leftsum < rightsum) {
                localvalue = leftsum + dfs(A, start, k, cache, prefix);;
            } else {
                // leftsum == rightsum;
                localvalue = leftsum + Math.max(dfs(A, start, k, cache, prefix), dfs(A, k + 1, end, cache, prefix));
            }
            localmax = Math.max(localmax, localvalue);
        }
        cache[start][end] = localmax;
        return cache[start][end];
    }
    
    private int getSum(int[] prefix, int i, int j) {
        return prefix[j + 1] - prefix[i];
    }
}

这题,其实也可以正着写:

class Solution {
    public int stoneGameV(int[] A) {
        int n = A.length;
        int[] prefix = new int[n + 1];
        prefix[0] = 0;
        for(int i = 1; i < prefix.length; i++) {
            prefix[i] = prefix[i - 1] + A[i - 1];
        }
        int[][] dp = new int[n][n];
        
        for(int len = 1; len <= n; len++) {
            for(int i = 0; i + len - 1 < n; i++) {
                int j = i + len - 1;
                // i..k...j
                int localmax = 0;
                for(int k = i; k < j; k++) {
                    int localscore = 0;
                    int leftsum = getSum(prefix, i, k);
                    int rightsum = getSum(prefix, k + 1, j);
                    
                    if(leftsum > rightsum) {
                        localscore = rightsum + dp[k + 1][j];
                    } else if(leftsum < rightsum) {
                        localscore = leftsum + dp[i][k];
                    } else {
                        // leftsum == rightsum;
                        localscore = leftsum + Math.max(dp[i][k], dp[k + 1][j]);
                    }
                    localmax = Math.max(localmax, localscore);
                }
                dp[i][j] = localmax;
            }
        }
        return dp[0][n - 1];
    }
    
    private int getSum(int[] prefix, int i, int j) {
        return prefix[j + 1] - prefix[i];
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值