动态规划-Predict the Winner

预测赢家的动态规划算法
本文介绍了一种利用动态规划预测游戏中先手玩家是否能赢得比赛的算法。通过递推关系式dp[i][j]=Math.max(nums[i]-dp[i+1][j],nums[j]-dp[i][j-1])计算先手玩家A与后手玩家B得分差值,最终判断先手玩家能否获胜。

2018-04-22 19:19:47

问题描述:

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.

Example 1:

Input: [1, 5, 2]
Output: False
Explanation: Initially, player 1 can choose between 1 and 2. 
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return False.

Example 2:

Input: [1, 5, 233, 7]
Output: True
Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.

问题求解:

首先我们如果穷举的话,是会出现重叠子问题的,比如A选left,B选left,A选right,B选right等同于A选right,B选right,A选left,B选left。因此适用于动态规划的方法来解决。现在问题就是如何建立这样的一个递推关系式。这条题目的动态规划建立是比较trick的,因此这里做一个介绍。

dp[i][j]:保存的是先手玩家A在i-j之间能获得的做高分数与后手玩家B的最高分数的差值。

初始条件:i == j时,dp[i][j] = nums[i],这也对应着长度为一的情况。

递推关系式:dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]),也就是说,对于当前的先手玩家,他既可以选择前面一个数,也可以选择后面一个数,那么后手玩家的范围就因此减少了,由于存储的是差值,因此可以得到上述的递推式。

    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 = 2; len <= n; len++) {
            for (int i = 0; i <= n - len; i++) {
                int j = i + len - 1;
                dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);
            }
        }
        return dp[0][n - 1] >= 0;
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/TIMHY/p/8909041.html

### KG Predict 实现应用 在知识图谱系统中,KG-Predict 是指利用预测算法来推断实体之间的潜在关系或属性。这类技术广泛应用于推荐系统、搜索引擎优化以及自然语言处理等领域。 #### 数据包分类中的机器学习方法 当前用于数据包分类的技术可以分为三类[^1]: - **监督学习**:通过预先标注好的数据集训练模型,使其能够识别并分类新的未知样本。 - **强化学习**:采用智能体根据环境反馈调整行为策略的方式,在线动态构建决策树以适应变化的网络状况。 - **无监督学习**:无需标签即可自动发现数据内在结构的方法,适用于探索未曾见过的新类型流量特征。 这些方法同样可被借鉴于改进 KG-Predict 的性能表现,特别是当面对复杂多变的知识图谱时,灵活运用上述三种方式能有效提升系统的鲁棒性和泛化能力。 #### 深度学习框架下的实现路径 现代深度神经网络架构为 KG-Predict 提供了强有力的支持工具。例如,可以通过引入注意力机制增强节点间关联性的捕捉;借助图卷积网络(GCN)挖掘局部邻域内的隐含模式;或是利用对比学习促进跨模态信息融合等手段提高预测精度。 具体来说,为了更好地结合不同来源的数据特性,研究者们提出了一种名为“混合”的体系结构设计思路[^3]。该方案旨在综合考虑多种因素的影响,从而达到更优的效果展示。 ```python import torch from transformers import BertModel, BertTokenizer class KGPredictor(torch.nn.Module): def __init__(self, pretrained_model='bert-base-chinese'): super(KGPredictor, self).__init__() self.bert = BertModel.from_pretrained(pretrained_model) def forward(self, input_ids, attention_mask=None): outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask) return outputs.last_hidden_state[:, 0, :] ``` 此代码片段展示了如何基于 BERT 构建一个简单的 KG-Predict 模型实例。实际应用场景下可能还需要针对特定任务需求做更多定制化的开发工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值