博客
关于我
LeetCode 486. 预测赢家(dp)
阅读量:226 次
发布时间:2019-03-01

本文共 903 字,大约阅读时间需要 3 分钟。

题意

给定一个表示分数的非负整数数组,玩家1和玩家2将按照规则轮流从数组两端拿取分数。玩家1先手,随后玩家2从剩余的另一端拿取,依此类推,直到分数全部拿完。最终,总分数较高的玩家获胜。如果两人的总分数相等,玩家1仍为赢家。

解法

这个问题可以通过动态规划来解决。我们定义d[i][j]为从数组的第i个元素到第j个元素这段区间中,当前先手玩家能够获得的最大分数。递归关系式如下:

d[i][j] = max(a[i] - d[i+1][j], a[j] - d[i][j-1])

其中,a[i]表示当前玩家从左端拿取的分数,而a[j]表示从右端拿取的分数。玩家会选择使自己总分数最大的选项,即max(a[i] - d[i+1][j], a[j] - d[i][j-1])。

代码

class Solution {private:    int d[22][22];    int a[22];    int dp(int l, int r) {        if (l == r) {            return a[l];        }        if (d[l][r] != -1) {            return d[l][r];        }        return d[l][r] = std::max(a[l] - dp(l + 1, r), a[r] - dp(l, r - 1));    }    bool PredictTheWinner(std::vector
aa) { int n = aa.size(); for (int i = 0; i < n; ++i) { a[i+1] = aa[i]; } dp(1, n); return d[1][n] >= 0; }};

这个代码定义了一个动态规划数组d[l][r],用于存储从位置l到r的最大分数差值。通过递归调用,计算出每个子区间的最优策略,最终判断玩家1是否能成为赢家。

转载地址:http://mwuv.baihongyu.com/

你可能感兴趣的文章
python | authlib,一个强大的 Python 库!
查看>>
python | awswrangler,一个高效的 Python 库!
查看>>
python | bashplotlib,一个有趣的Python库!
查看>>
python | bentoml,一个超级厉害的 模型部署 Python 库!
查看>>
python调用jar包的模块_python调用jar包
查看>>
python | black,一个神奇的 代码格式化工具 Python 库!
查看>>
python | bleach,一个超强的 Python 库!
查看>>
python | cartopy,一个有趣的 Python 库!
查看>>
python | cloud-init,一个实用的 云计算 Python 库!
查看>>
python | code2flow,一个神奇的 Python 库!
查看>>
python | cudf,一个超实用的 Python 库!
查看>>
python | daphne,一个非常nice的 Python 库!
查看>>
python调用halcon
查看>>
python | doit,一个非常实用的 Python 库!
查看>>
python | easyocr,一个超厉害的 关于OCR的 Python 库!
查看>>
python | fastFM,一个高级的 Python 库!
查看>>
python | feature_engine,一个实用的 Python 库!
查看>>
python | filelock,一个超酷的 Python 库!
查看>>
python | fire,一个强大的 Python 库!
查看>>
python | flanker,一个神奇的 Python 库!
查看>>