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. (六)QDialog,QMessageBox,QFileDialog,QColorDialog颜色,QFontDialog字体

    QDialog 对话框: 1.模态对话框: QDialog dlg(this); // 显示模态对话框 exec ,后面的不可操作 dlg.exec(); // 阻塞 2.非模态对话框: QDialo ...

  2. 使用zabbix监控nginx的活动连接数

    使用zabbix监控nginx的活动连接数 1.方法简述 zabbix可以自定义很多监控,只要是能通过命令获取到相关的值,就可以在zabbix的监控中增加该对象进行监控,在zabbix中,该对象称之为 ...

  3. apue——无缓冲读写操作

    stdrw.c文件 #include "apue.h" #define BUFFSIZE 4096 #include <stdio.h> int main(int ar ...

  4. Evaluation of Forwarding Efficiency in NFV-Nodes Toward Predictable Service Chain Performance

    文章名称:Evaluation of Forwarding Efficiency in NFV-Nodes Toward Predictable Service Chain Performance 发 ...

  5. Nuxt.js笔记

    前置知识 SSR服务器渲染 Vue SSR(server side rendering)服务端渲染 和 Vue SPA(single page application)单页应用 Vue SSR-> ...

  6. Docker实践之02-使用镜像及定制

    目录 一.获取镜像 二.使用镜像启动容器实例 三.列出镜像 四.删除本地镜像 五.定制镜像 通过commit命令定制镜像 通过Dockerfile定制镜像 docker build的工作原理 dock ...

  7. ajax跨域例子

    例子 https://github.com/ruanyf/react-demos/blob/master/demo12/index.html 此网页代码运行在本地, 是可以访问 github 数据的. ...

  8. centos7-aliyun

    # 安装EPEL和IUS软件源 yum install epel-release -y yum install https://centos7.iuscommunity.org/ius-release ...

  9. CentOS 7 yum 安装php5.6

    注意--enablerepo=remi --enablerepo=remi-php56这两个参数,指定源的意思 配置yum源 追加CentOS 6.5的epel及remi源. # rpm -Uvh h ...

  10. C++与蓝图互调

    Kismet库 蓝图方法cpp使用 例:打LOG:Print String 蓝图节点的鼠标tips:Target is Kismet System Library #include "Run ...