[leetcode] Stone Game

Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].

The objective of the game is to end with the most stones.  The total number of stones is odd, so there are no ties.

Alex and Lee take turns, with Alex starting first.  Each turn, a player takes the entire pile of stones from either the beginning or the end of the row.  This continues until there are no more piles left, at which point the person with the most stones wins.

Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.

Example 1:

Input: [5,3,4,5]
Output: true
Explanation: 
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.

Note:

  1. 2 <= piles.length <= 500
  2. piles.length is even.
  3. 1 <= piles[i] <= 500
  4. sum(piles) is odd.

思路:区间型动态规划,f[i][j] 表示的物理意义是:面对i 到j的石头,下棋子的人,我可以拿到的最大的与对手的数字差

f[i][j] = max {a[i] - f[i + 1][j], a[j] - f[i][j - 1]} , 最后如果f[0][n - 1] >= 0 代表先手alex能够拿到正数,也就是比lee拿得多,从而赢;

初始化: f[i][i] = a[i],  就面对一个数字;

计算顺序:

长度1: f[0][0] f[1][1] f[2][2]....f[n - 1][n - 1]

长度2:f[0][1],.....f[n - 2][n-1]

...

长度N:f[0][n - 1]

class Solution {
    public boolean stoneGame(int[] A) {
        int n = A.length;
        int[][] dp = new int[n][n];
        for(int i = 0; i < n; i++) {
            dp[i][i] = A[i];
        }
        // len == 1,已经计算过了;
        for(int len = 2; len <= n; len++) {
            for(int i = 0; i + len - 1 < n; i++) {
                int j = i + len - 1;
                dp[i][j] = Math.max(A[i] - dp[i + 1][j], A[j] - dp[i][j - 1]);
            }
        }
        return dp[0][n - 1] > 0;
    }
}

思路2:看了花花的视频,感觉还是min-max的思路,dfs+memo cache比较好理解,比较好想。

score(A, l, r)代表面对 s[l]...s[r] 区间,我能够取得的相对的最大值,也就是我的值减去对手的值,要最大;博弈型都是相互min对方的解,max自己的解; 不用cache,会超时TLE; 加入cache, O(N^2) ,因为总共有r, l N^2个解,每个解用了cache就是O(1),所以就是O(N^2).

class Solution {
    public boolean stoneGame(int[] A) {
        int n = A.length;
        int[][] cache = new int[n][n];
        return dfs(A, 0, n - 1, cache) > 0;
    }
    
    private int dfs(int[] A, int start, int end, int[][] cache) {
        if(start > end) {
            return 0;
        }
        if(start == end) {
            return A[start];
        }
        if(cache[start][end] != 0) {
            return cache[start][end];
        }
        cache[start][end] = Math.max(A[start] - dfs(A, start + 1, end, cache),
                                     A[end] - dfs(A, start, end - 1, cache));
        return cache[start][end];
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值