All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0401_0500.s0486_predict_the_winner.Solution Maven / Gradle / Ivy

There is a newer version: 1.24
Show newest version
package g0401_0500.s0486_predict_the_winner;

// #Medium #Array #Dynamic_Programming #Math #Recursion #Game_Theory

@SuppressWarnings("java:S3012")
public class Solution {
    public boolean predictTheWinner(int[] nums) {
        int n = nums.length;
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; i++) {
            dp[i][i] = nums[i];
        }
        for (int len = 1; len < n; len++) {
            for (int i = 0; i < n - len; i++) {
                int j = i + len;
                dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);
            }
        }
        return dp[0][n - 1] >= 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy