您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页486. Predict the Winner

486. Predict the Winner

来源:化拓教育网

解题思路:

  1. dp[j] 代表数组从index of 0 到 index of j plaryer1 比player2大多少,然后就能通过
    dp【j】是否>0 判断plaryer1大多少
  2. 核心是dp[j] = Math.max(nums[i] - dp[j], num[j] - dp[j - 1]); nums[i] 代表数组value at index of 0, nums[j] represents the arrray's value at index of j. dp[j] represents the winner value (player1 - player2) from index of i +1 to j; dp[j - 1] represents the winner value (player1 - player2) from index of i to j - 1.

class Solution {
public boolean PredictTheWinner(int[] nums) {

    int len = nums.length;
    int[] dp = new int[len];
    for(int i = len - 1; i >= 0; i--){
        for (int j = i; j < len; j++ ){
            if(i == j)
                dp[j] = nums[i];
            else
                dp[j] = Math.max(nums[i] - dp[j], nums[j] - dp[j - 1]);
        }
    }
    return dp[len - 1] >= 0;
}

}

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务