【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 ...
随机推荐
- Window平台下tree 命令使用
WIndow 平台要想打印目录树,可以用cmd工具或者power shell 的tree命令实现 tree 命令格式和参数: TREE [drive:][path] [/F] [/A] /F 显示每个 ...
- $emit 和 $on 进行平行组件之间的传值
效果图: 注:$emit 和 $on 的事件必须在一个公共的实例上,才能够触发: $emit 触发 $on 接收 需求: 1.有A.B.C三个组件,同时挂载到入口组件中: 2.将A组件中的数据传递到C ...
- 获取mysql数据库表字段的备注信息
SELECT COLUMN_NAME as field_name , COLUMN_COMMENT as remark FROM information_schema.COLUMNS WHERE T ...
- 第一周作业—N42-虚怀若谷
一.Linux发行版描述. Linux发行版主要有三个分支:Slackware.Debian.Redhat: (1) Slackware: SUSE:基于Slackware二次开发的一款Linux,主 ...
- Codeforces 837D--Round Subset (DP)
原题链接:http://codeforces.com/contest/837/problem/D 题意:在n个数字中,取k个数,使这些数的乘积后缀“0”的个数最大,输出后缀0的最大数量. 思路:显然只 ...
- Invalid bound statement (not found)错误
都对着,为什么会报这个错呢,mapper也拿到了,为什么查询时出错呢,最后看target里编译的文件发现少了mapping,xml没编译过去. 我的目录结构:dao层都编译过去了,但mapper.xm ...
- java高并发解决思路
一个小型的网站,比如个人网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构.性能的要求都很简单,随着互联网业务的不断丰富,网站 ...
- 「LibreOJ β Round」ZQC 的手办
https://loj.ac/problem/504 一类套路题. 首先这个玩意可以两个logn树套树做.... naive地,把区间内的所有数拿出来放进堆里.不断取出. 太多了. 所以开始只保留那初 ...
- JSON的android应用实例
JSON的android应用实例 Json在线解析器 下面是直接通过JUnit来测试直接通过API来解析Json数据 1.普通键值对象 2.Json数组对象 3.Json数组对象
- SSAS MDX语句 期末查询简单示例
WITH Member [Measures].[num Last Day of Month] AS( [时间].[YQMD].CurrentMember.LastChild,[Measures].[门 ...