Diagonal

394t. Coins in a Line

There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins.

Could you please decide the first play will win or lose?

/**
 * Statement: dp[i], when i coins last, if the person now taking the coin could win
 * Func: dp[i] = true if dp[i-1] or dp[i-2] is false
 * Init: dp[0] = false, dp[1] = true;
 * Ans: dp[n]
 * 
 */
public boolean firstWillWin(int n) {
    if(n==0) return false;
    if(n==1) return true;

    boolean[] dp = new boolean[n+1];

    dp[0] = false;
    dp[1] = true;

    for(int i=2; i<=n; i++){
        if(dp[i-1]==false || dp[i-2]==false) dp[i] = true;
    }

    return dp[n];
}

395t Coin In Line II

There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins.

Could you please decide the first player will win or lose?

/**
  * Statement: dp[i], when i coins last, the money the person now taking the coin more than opponent
  * Func: dp[i] = max(sum[i]-dp[i-1], sum[i]-dp[i-2]);
  * Init: postSum, 剩i張時sum[i]
  * Ans: dp[n] > sum/2
  */

public boolean firstWillWin(int[] values) {
    int n = values.length;
    int[] sum = new int[n+1];
    int[] dp = new int[n+1];
    for(int i=1; i<=n; i++){
        sum[i] = sum[i-1]+values[n-i];
    }

    dp[1] = values[n-1];

    for(int i=2; i<=n; i++){
        dp[i] = Math.max(sum[i]-dp[i-1], sum[i]-dp[i-2]);
    }

    return dp[n] > sum[n]/2;
}

486. Predict the Winner(由下往上更新)

An array of numbers, 2 players take pile from each side by turns. Return if player 1 can win

  • State: dp[i][j] = pick from nums[i] to nums[j], the number you has more than competitor

  • Func: dp[i][j] = max(nums[i] - dp[i+1][j], nums[j] - dp[i][j-1])

  • Ans: if dp[0][n-1] > 0

Explain

public boolean PredictTheWinner(int[] nums) {
    int[][] dp = new int[nums.length][nums.length];
    for (int s = nums.length; s >= 0; s--) {
        for (int e = s + 1; e < nums.length; e++) {
            int a = nums[s] - dp[s + 1][e];
            int b = nums[e] - dp[s][e - 1];
            dp[s][e] = Math.max(a, b);
        }
    }
    return dp[0][nums.length - 1] >= 0;
}
  • 1D space solution

public boolean PredictTheWinner(int[] nums) {
    int[] dp = new int[nums.length];
    for (int s = nums.length; s >= 0; s--) {
        for (int e = s + 1; e < nums.length; e++) {
            int a = nums[s] - dp[e];
            int b = nums[e] - dp[e - 1];
            dp[e] = Math.max(a, b);
        }
    }
    return dp[nums.length - 1] >= 0;
}

877 Stone Game(對角線更新)

A pile of stones, 2 players take pile from each side by turns. Return if player 1 has more stones

  • State: dp[i][j] = pick from piles[i] to piles[j], the stone you has more than competitor

  • Func: dp[i][j] = max(p[i] - dp[i+1][j], p[j] - dp[i][j-1])

  • Init: dp[i][i] = p[i];

  • Ans: if dp[0][n-1] > 0

public boolean stoneGame(int[] piles) {
    int n = piles.length;
    int[][] dp = new int[n][n];

    for(int i=0; i<n; i++){
        dp[i][i] = piles[i];
    }

    for(int d=1; d<n; d++){
        for(int i=0; i<n-d; i++){
            dp[i][i+d] = Math.max(piles[i]-dp[i+1][i+d], piles[i+d]-dp[i][i+d-1]);
        }
    } 
    return dp[0][n-1] >0;
}

Last updated