思路:

dp,用记忆化搜索比较好实现。

实现:

  1. class Solution
  2. {
  3. public:
  4. int dfs(vector<int>& sum, int cur, int M, vector<vector<int>>& dp)
  5. {
  6. int n = sum.size();
  7. if (n - cur <= * M) return sum[n - ] - sum[cur];
  8. if (dp[cur][M] != -) return dp[cur][M];
  9. int ans = INT_MIN;
  10. for (int i = ; i <= min( * M, n - cur); i++)
  11. {
  12. int tmp = dfs(sum, cur + i, max(M, i), dp);
  13. ans = max(ans, sum[cur + i - ] - sum[cur] + sum[n - ] - sum[cur + i - ] - tmp);
  14. }
  15. return dp[cur][M] = ans;
  16. }
  17. int stoneGameII(vector<int>& piles)
  18. {
  19. int n = piles.size();
  20. vector<int> sum(n + , );
  21. vector<vector<int>> dp(n + , vector<int>(n + , -));
  22. for (int i = ; i <= n; i++) sum[i] = sum[i - ] + piles[i - ];
  23. int res = dfs(sum, , , dp);
  24. return res;
  25. }
  26. }

leetcode1140 Stone Game II的更多相关文章

  1. HDU4388:Stone Game II(博弈+思维)

    Stone Game II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. hdu 4388 Stone Game II sg函数 博弈

    Stone Game II comes. It needs two players to play this game. There are some piles of stones on the d ...

  3. hdu 4388 Stone Game II

    Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择 ...

  4. HDU 4388 Stone Game II {博弈||找规律}

    Stone Game II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. Leetcode--Last Stone Weight II

    Last Stone Weight II 欢迎关注H寻梦人公众号 You are given an array of integers stones where stones[i] is the we ...

  6. LeetCode 1049. Last Stone Weight II

    原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each ...

  7. LeetCode 1140. Stone Game II

    原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with pile ...

  8. Stone Game II

    Description There is a stone game.At the beginning of the game the player picks n piles of stones in ...

  9. [hdu4388]Stone Game II

    不管是否使用技能,发现操作前后所有堆二进制中1的个数之和不变.那么对于一个堆其实可以等价转换为一个k个石子的堆(k为该数二进制的个数),然后就是个nim游戏. 1 #include<bits/s ...

随机推荐

  1. js中数组元素的添加和删除

    js中数组元素常用添加方法是直接添加.push方法以及unshift方法 删除方法则是delete.pop.shift 集修改方法为一身的则是splice 1.添加: (1)直接添加通常都是这样 va ...

  2. Ftp客户端需要TSL功能的文件上传

    Ftp客户端需要TSL功能 1.由于最近做了一个项目,需要把打包的文件传输到对方的FTP服务器上,但是用普通的java连接ftp客户端总是连接不上去,对方却说ftp客户端需要开通TSL功能. 直接上代 ...

  3. 1040 too many connections

    先重启mysql. 登录成功后执行以下语句查询当前的最大连接数:select VARIABLE_VALUE from information_schema.GLOBAL_VARIABLES where ...

  4. wangEditor编辑器从word粘贴公式

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-editor的二进制流保存为例:打开umeditor.js,找到UM ...

  5. 【转】使用 Ansible 实现数据中心自动化管理

    长久以来,IT 运维在企业内部一直是个耗人耗力的事情.随着虚拟化的大量应用.私有云.容器的不断普及,数据中心内部的压力愈发增加.传统的自动化工具,往往是面向于数据中心特定的一类对象,例如操作系统.虚拟 ...

  6. 解决Navicat无法连接到centos上的MySQL,但命令行可以,修改权限,MySQL密码权限受限:ERROR 1820 (HY000) ERROR 1819 (HY000)

    问题分析 查看MySQL文档发现5.7版本后加入了对用户密码严格的管理规范,具体设置字段如下: validate_password_dictionary_file #插件用于验证密码强度的字典文件路径 ...

  7. meshing-simple_block

    原视频下载地址:https://yunpan.cn/cqjeSzP7s93Pc  访问密码 aaff

  8. 从java字节码角度看线程安全性问题

    先看代码: package com.roocon.thread.t3; public class Sequence { private int value; public int getNext(){ ...

  9. android.mk-include

    -- include $(BUILD_STATIC_LIBRARY)表示编译成静态库 include $(BUILD_SHARED_LIBRARY)表示编译成动态库. include $(BUILD_ ...

  10. 利用pathMeasure实现路径动画

    package com.loaderman.customviewdemo; import android.animation.ValueAnimator; import android.content ...