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

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

题意:

给定一个表示分数的非负整数数组。玩家 1 从数组任意一端拿取一个分数,随后玩家 2 继续从剩余数组任意一端拿取分数,然后玩家 1 拿,…… 。每次一个玩家只能拿取一个分数,分数被拿取之后不再可取。直到没有剩余分数可取时游戏结束。最终获得分数总和最多的玩家获胜。给定一个表示分数的数组,预测玩家1是否会成为赢家。你可以假设每个玩家的玩法都会使他的分数最大化。数据范围:1 <= 给定的数组长度 <= 20.数组里所有分数都为非负数且不会大于 10000000 。如果最终两个玩家的分数相等,那么玩家 1 仍为赢家。

解法:

比较分数可以转化为差值,当两者分数差值>=0时,玩家1胜利.令d[i][j]表示[i,j]先手选完能获得的最大值.那么d[i][j]=max(a[i]-d[i+1][j],a[j]-d[i][j-1]).最后判断d[1][n]是否>=0即可.

code:

class Solution {   public:    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]=max(a[l]-dp(l+1,r),a[r]-dp(l,r-1));    }    bool PredictTheWinner(vector
& aa) { memset(d,-1,sizeof d); int n=aa.size(); for(int i=0;i
=0; }};

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

你可能感兴趣的文章
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>