博客
关于我
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/

你可能感兴趣的文章
nginx+tomcat单个域名及多个域名配置
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
nginxWebUI runCmd RCE漏洞复现
查看>>
nginx_rtmp
查看>>
Vue中向js中传递参数并在js中定义对象并转换参数
查看>>
Nginx、HAProxy、LVS
查看>>
nginx一些重要配置说明
查看>>
Nginx一网打尽:动静分离、压缩、缓存、黑白名单、跨域、高可用、性能优化......
查看>>