【leetcode】948. Bag of Tokens
题目如下:
You have an initial power
P, an initial score of0points, and a bag of tokens.Each token can be used at most once, has a value
token[i], and has potentially two ways to use it.
- If we have at least
token[i]power, we may play the token face up, losingtoken[i]power, and gaining1point.- If we have at least
1point, we may play the token face down, gainingtoken[i]power, and losing1point.Return the largest number of points we can have after playing any number of tokens.
Example 1:
Input: tokens = [100], P = 50
Output: 0Example 2:
Input: tokens = [100,200], P = 150
Output: 1Example 3:
Input: tokens = [100,200,300,400], P = 200
Output: 2Note:
tokens.length <= 10000 <= tokens[i] < 100000 <= P < 10000
解题思路:本题可以用贪心算法。即可以得分的时候优先选择当前最小的token[i]得分,不能得分的时候选择当前可选的最大的token[i]换取power。所以只需要将tokens排序,分别从两端选择当前最小/最大的元素即可。
代码如下:
class Solution(object):
def bagOfTokensScore(self, tokens, P):
"""
:type tokens: List[int]
:type P: int
:rtype: int
"""
tokens.sort()
low = 0
high = len(tokens) - 1
res = 0
point = 0
while low <= high:
if P >= tokens[low]:
point += 1
P -= tokens[low]
low += 1
res = max(res,point)
elif point > 0:
point -= 1
P += tokens[high]
high -= 1
else:
low += 1
return res
【leetcode】948. Bag of Tokens的更多相关文章
- 【LeetCode】948. Bag of Tokens 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- mui滚动区域的实现
mui框架实现页面中间区域滚动,头部和底部固定不动,要滚动的区域一定要有mui-scroll-wrapper 和 mui-scroll 包裹 <div class="mui-conte ...
- 【KeepAlive】Http--Keep-Alive及Tcp--Keepalive
Keep-Alive模式: Http协议采用“请求-应答”模式,当使用普通模式,即非Keep-Alive模式时,每个请求/应答,客户端和服务器都要新建一个连接,完成之后立即断开连接:当使用Keep-A ...
- CSP-S2019退役记/爆内存记
DAY 0 准备出发. 出发前教练说遇到事不慌,打电话,又听教练说了说历年赶车经历. 然后这趟路上居然没有什么大事. 在车上有些闲,于是就和其他人聊了会天,聊着聊着没意思,就用手机翻博客园. 这样就不 ...
- PHP之namespace小结
命名空间的使用 在声明命名空间之前唯一合法的代码是用于定义源文件编码方式的 declare 语句.所有非 PHP 代码包括空白符都不能出现在命名空间的声明之前. PHP 命名空间中的类名可以通过三种方 ...
- sonarqube6.7.1使用
1.插件安装 方法1.登入sonarqube-web安装 admin/admin 配置--应用市场--全部 英文片:administration--configuration--marketplace ...
- 2018-2019 2 20165203 《网络对抗技术》Exp8 Web基础
2018-2019 2 20165203 <网络对抗技术>Exp8 Web基础 实验要求 1.本实践的具体要求有: (1) Web前端HTML(0.5分) 能正常安装.启停Apache.理 ...
- git 小错误
(一)在本地直接修改文件,提交后出现(master|REBASE 1/2).由于文件冲突所以导致各种报错. 在git pull --rebase origin master后 error: Pulli ...
- 八. jenkins参数化构建 git分支
使用git管理代码时往往会有多分支开发,这时部署通过修改配置不试用.可以通过参数化构建. 1.通过字符参数方式 新建一个maven项目,选择参数化构建过程-字符参数,如下: 源码管理中配置如下: 其它 ...
- Linux ftp安装
ftp安装部分,操作步骤如下: 可以使用yum命令直接安装ftp # yum install vsftpd ftp服务的开启与关闭命令: 开启:# /bin/systemctl start vsftp ...
- js的几个特殊的运算符略解
js运算符的一些特殊应用及使用技巧. 1. 是否包含指定字符: ~ ~"str1".indexOf("str2") 含义为:str1 被查找的字符串 str2 ...