486. Predict the Winner Add to List | Leetcode Dynamic Programming

本文介绍了一种通过动态规划预测两位玩家游戏中胜者的算法。玩家轮流从数组两端选取整数值,目标是获得比对手更高的累计分数。文章详细阐述了使用二维数组存储中间结果的方法,避免重复计算,并给出了具体的C++实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.
Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.
这里写图片描述

Thinking

玩家1与玩家2先后轮流从队列nums头或尾取数字,最后数字总和最大的为赢家。用动态规划解决即可。
将问题从大往小逐渐拆分,设队列长度为len,创建一个二维数组dp[len][len],dp[begin][end]表示在nums[begin]到nums[end]之间范围内进行游戏,玩家1与玩家2之间的分数差值。当begin和end相等的时候,dp[begin][end]的值即为nums[begin](或者nums[end]),如果begin和end不等,那么如果取begin,结果为nums[begin] – dp[begin+1][end]; 如果取end,结果为nums[end] – dp[begin][end-1],dp[begin][end]取它俩中较大的一个,因此得到递归式:max(nums[beg] - partition(beg + 1, end), nums[end] - partition(beg, end - 1))。
为了实现动态规划,减少重复计算,需要用这个二维数组存储每个小问题的计算结果。
开始时,我们只知道对角线上的值恰好与Nums数列中的值一一对应。接下来要做的就是讲二维数组dp[len][len]的上三角填满。我们先从右下角开始,利用已知的数据求出小区间dp[b[][e]的值。

Solution

class Solution {
public:
    bool PredictTheWinner(vector<int>& nums) {
        int len = nums.size();
        if(len < 0) return false;
        int dp[len][len];
        for(int i = 0; i < len; i++){
            dp[i][i] = nums[i];
        }
        for(int b = len - 2; b >= 0; b--){
            for(int e = b + 1; e < len; e++){
                if(nums[b] - dp[b + 1][e] >= nums[e] - dp[b][e - 1]) dp[b][e] = nums[b] - dp[b + 1][e];
                else dp[b][e] = nums[e] - dp[b][e - 1];
            }
        }
        return dp[0][len - 1] >=0;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值