leetcode877 Stone Game

思路:贪心策略显然不对 因此需要列举所有情况 使用dp降低时间复杂度(此题有数学解 可以证明Alex一定赢)

alex和lee是在头和尾轮流拿石头 dp记录时需要记录两端 因此是一个二维数组dp[ left][right];

以[5,3,4,5]为例 如果拿左边 剩下的是[3,4,5] ,dp[left][right] =piles[left]+dp[left+1][right] ? 因为Lee还要拿 所以 这个是不对的 剩下的[3,4,5]lee要拿掉头尾中的一个,实际上alex只能在[3,4]或者[4,5]中选取 因此dp[left][right] =piles[left]+max(dp[left+1][right-1],dp[left+2,right])
如果拿右边的 dp[left][right]=piles[right]+max(dp[left+1][right-1],dp[left,right-2]);
在左右中选一个最大的:
dp[left][right]=max(piles[left]+max(dp[left+1][right-1]dp[left+2,right]),piles[right]+max(dp[left+1][right-1],dp[left,right-2]);

class Solution {public:using VV=vector<vector<int> >;using V=vector<int>;bool stoneGame(vector<int>& piles) {int n=piles.size();vector<vector<int> > dp(n,vector<int>(n,-1));vector<int> alex(n,0);// 标记alex选取的pile 可以不要 用piles总和减去alexsumint alexsum=maxsum(dp,alex,piles,0,n-1);int leesum=0;// 可以不用这么写 直接piles总和减去alexsum即可for(int i=0;i<n;++i){if(!alex[i]){leesum+=piles[i];}}//cout<<alexsum<<' '<<leesum<<endl;if(alexsum>leesum){return true;}else{return false;}}int maxsum(VV& dp,V& alex,V& piles,int left,int right){if(left>right)return 0;if(dp[left][right]!=-1)return dp[left][right];int choosel=piles[left]+max(maxsum(dp,alex,piles,left+1,right-1),maxsum(dp,alex,piles,left+2,right));int chooser=piles[right]+max(maxsum(dp,alex,piles,left+1,right-1),maxsum(dp,alex,piles,left,right-2));if(choosel>chooser){alex[left]=1;dp[left][right]=choosel;}else{dp[left][right]=chooser;alex[right]=1;}//cout<<"dp["<<left<<"]"<<"["<<right<<"]="<<dp[left][right];return dp[left][right];}
};

leetcode877相关推荐

  1. LeetCode-877 石子游戏

    动态规划 LeetCode-877 石子游戏 题目链接:LeetCode-877 题目大意:从一排石子堆最左面或者最右面取石子 每次一堆必须全部取走 最后石子总数多的获胜 问最后先手可不可以获胜 解题 ...

  2. leetcode877.StoneGame

    题目:一个数组,俩人从里面取数,要么从最左边取,要么从最右边取,直至把所有数取完,若第一个人取得所有数之和比第二个人取得数之和大,则为true 输入:一个整型数组 输出:true or false 别 ...

  3. 动态规划---石子游戏

    动态规划---石子游戏 石子游戏(leetcode877) 石子游戏(leetcode1140) 石子游戏(leetcode1686) 石子游戏(leetcode877) 题目描述 亚历克斯和李用几堆 ...

  4. Acwing LeetCode 题目分类——配套基础课进阶课

    LeetCode 题目分类--配套基础课进阶课 1.基础 二分(满足一个条件的最值问题) LeetCode33 https://leetcode.com/problems/search-in-rota ...

  5. 动态规划(持续更新、整理)

    动态规划 记忆化搜索 不同路径:leetcode-62 分割回文串:leetcode-131 单词拆分II:leetcode-140 戳气球:leetcode-312 零钱兑换:leetcode-32 ...

  6. 数学(持续更新、整理)

    两数相加:leetcode-2 Z字形变换:leetcode-6 整数反转:leetcode-7 Pow(x, n):leetcode-50 排列序列:leetcode-60 爬楼梯:leetcode ...

  7. leetcode1049. 最后一块石头的重量 II(java)

    最后一块石头的重量 II leetcode1049. 最后一块石头的重量 II 题目描述 解题思路 解法一 暴力递归 解法二 动态规划 动态规划专题 leetcode1049. 最后一块石头的重量 I ...

最新文章

  1. 关于欧盟的芯片法案,ASML是这样看的!
  2. Python基础-模块
  3. 剖析Caffe源码之ImageDataLayer
  4. Mysql设置binlog过期时间并自动删除
  5. Prototype使用$()函数
  6. 3月7日学习内容整理:restframework的解析器组件
  7. FRR BGP 协议分析 3 --- FSM 状态机
  8. 使用JSP做一个小小的项目(一)
  9. 分析微博游戏名人数据
  10. magick++配置
  11. OLED屏幕的手机和LCD屏幕的手机,到底哪个好?
  12. 利盟 x466 双面打印问题之正反面颠倒
  13. c++ mfc实现中英文菜单的动态切换
  14. 页面表单数据返显方法
  15. linux grub配置文件丢失,linux grub.conf文件丢失恢复
  16. linux网卡通信延迟高,Linux性能优化-网络请求延迟变大
  17. php数据库数组json,PHP 从数据库中取出数据并存为Json数据
  18. python分享文件_Python文件
  19. python大气校正_Python调用C2RCC实现批量数据的大气校正
  20. 无人化,自动化技术影响世界

热门文章

  1. 机器学习论文:《LightGBM: A Highly Efficient Gradient Boosting Decision Tree》
  2. 一天半的黄山之行总结
  3. 驱动力3.0,动力全开~
  4. 第三天python作业题
  5. Borax.Lunardate:中国农历日期
  6. java 深入剖析ThreadLocal
  7. MAX40026 280ps高速比较器开发资料原理图源代码
  8. 服务器类型,服务器名称,数据库,数据库服务实例之间的关系
  9. 情绪:感觉、思考和沟通(无助益情绪通常由哪些非理性思考带来的、以及怎么减少这种无助益情绪...
  10. 移动UI 设计有哪些色彩级别