https://leetcode.com/problems/bag-of-tokens/

一开始觉得应该是个dp 题,把所有结果搜出来然后max 一下。实现以后发现组合太多了,非常慢,即使加上memorization 也是TLE

var hash = function(arr, p, s) {
arr = arr.sort();
return arr.reduce((p, c)=>p+"-"+c, "") + "_" + p + "_" + s;
}
let m = {};
var iter = function(tokens, p, s) {
if (tokens.length == 0) return s;
let h = hash(tokens, p, s);
if (m[h] != void 0) return m[h];
let result = s;
for (let i = 0; i < tokens.length; ++i) {
//option1, if we have at leaset token[i] power
if (p >= tokens[i]) {
result = Math.max(result, iter(tokens.filter((x,k)=>k!=i), p-tokens[i], s+1));
} //option2, if we have at least 1 point
if (s >= 1) {
result = Math.max(result, iter(tokens.filter((x,k)=>k!=i), p+tokens[i], s-1));
}
}
m[h] = Math.max(result, m[h]||0);
return result;
} var bagOfTokensScore = function(tokens, P) {
return iter(tokens, P, 0);
};

看了答案发现是用greedy,然而也没有证明为啥greedy 就是最优解。

948. Bag of Tokens的更多相关文章

  1. 【leetcode】948. Bag of Tokens

    题目如下: You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can ...

  2. 【LeetCode】948. Bag of Tokens 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

  3. [Swift]LeetCode948. 令牌放置 | Bag of Tokens

    You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be us ...

  4. 贪心-Bag of Tokens

    2020-01-20 22:32:28 问题描述: 问题求解: 双指针 + 贪心. public int bagOfTokensScore(int[] tokens, int P) { Arrays. ...

  5. Weekly Contest 112

    945. Minimum Increment to Make Array Unique Given an array of integers A, a move consists of choosin ...

  6. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)

    Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...

  9. 机器学习---文本特征提取之词袋模型(Machine Learning Text Feature Extraction Bag of Words)

    假设有一段文本:"I have a cat, his name is Huzihu. Huzihu is really cute and friendly. We are good frie ...

随机推荐

  1. 使用Excel VBA编程将网点的百度坐标转换后标注到高德地图上

    公司网点表存储的坐标是百度坐标,现需要将网点位置标注到高德地图上,研究了一下高德地图的云图数据模版 http://lbs.amap.com/yuntu/reference/cloudstorage和坐 ...

  2. 函数内部还是不要使用 strtok()

    今天在调试程序的时候,遇到一个奇怪的事情,一开始担心是代码存在内存溢出引起的,花了半个小时没找到原因. 在吃饭的时候,突然想起可能是 strtok() 引起的,查找调用的函数,果然发现在函数中使用了  ...

  3. python 向量化

    study from : https://www.jianshu.com/p/ad8933dd6407

  4. 处理 Vue 单页面应用 SEO

    由于在vue单页应用中title只设定在入口文件index.html,如果切换路由,title怎么更换? 在路由router中设置meta: { path:'/chooseBrand', compon ...

  5. python flsak 框架

    1.flask  轻量级微型web框架 优点:微框架.简单.可扩展 将flask变量实例化到app变量中 如果想要flask自动加载修改后的代码,要app.run(debug=True) 2.路由和视 ...

  6. JSP+MySQL验证登录的实现方式

    用IDEA连接MySQL验证登录实现方式核心部分代码 用setString的方法对从数据库中的提取的信息经行比对: try { Class.forName("com.mysql.jdbc.D ...

  7. 20164305 徐广皓 Exp3 免杀原理与实践

    免杀原理及基础问题回答 实验内容 任务一:正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己利用shellcode编程等免杀工具或技巧 使用msf编码器生 ...

  8. axios formData提交数据 && axios设置charset无效???

    但是这样会出现一个问题,什么问题呢? 我设置了请求头编码utf-8,但是没生效 content-type里面没有出现utf-8???????查了很多资料,说这是axios固有的bug,我....... ...

  9. Spring Cloud 2-Hystrix DashBoard仪表盘(五)

    Spring Cloud  Hystrix DashBoard  1.监控系统配置 pom.xml application.yml Application.java 2.被监控服务配置 pom.xml ...

  10. JAVA学习笔记(1)—— eclipse自动补全和主题及字体配置

    1.自动补全功能 (1)打开 Eclipse -> Window -> Perferences (2)选择Java -> Editor -> Content Assist -& ...