题目如下:

You have an initial power P, an initial score of 0 points, 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, losing token[i] power, and gaining 1 point.
  • If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.

Return the largest number of points we can have after playing any number of tokens.

Example 1:

Input: tokens = [100], P = 50
Output: 0

Example 2:

Input: tokens = [100,200], P = 150
Output: 1

Example 3:

Input: tokens = [100,200,300,400], P = 200
Output: 2

Note:

  1. tokens.length <= 1000
  2. 0 <= tokens[i] < 10000
  3. 0 <= 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的更多相关文章

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

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

  2. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. Window平台下tree 命令使用

    WIndow 平台要想打印目录树,可以用cmd工具或者power shell 的tree命令实现 tree 命令格式和参数: TREE [drive:][path] [/F] [/A] /F 显示每个 ...

  2. $emit 和 $on 进行平行组件之间的传值

    效果图: 注:$emit 和 $on 的事件必须在一个公共的实例上,才能够触发: $emit 触发 $on 接收 需求: 1.有A.B.C三个组件,同时挂载到入口组件中: 2.将A组件中的数据传递到C ...

  3. 获取mysql数据库表字段的备注信息

    SELECT COLUMN_NAME as field_name , COLUMN_COMMENT as remark  FROM information_schema.COLUMNS WHERE T ...

  4. 第一周作业—N42-虚怀若谷

    一.Linux发行版描述. Linux发行版主要有三个分支:Slackware.Debian.Redhat: (1) Slackware: SUSE:基于Slackware二次开发的一款Linux,主 ...

  5. Codeforces 837D--Round Subset (DP)

    原题链接:http://codeforces.com/contest/837/problem/D 题意:在n个数字中,取k个数,使这些数的乘积后缀“0”的个数最大,输出后缀0的最大数量. 思路:显然只 ...

  6. Invalid bound statement (not found)错误

    都对着,为什么会报这个错呢,mapper也拿到了,为什么查询时出错呢,最后看target里编译的文件发现少了mapping,xml没编译过去. 我的目录结构:dao层都编译过去了,但mapper.xm ...

  7. java高并发解决思路

    一个小型的网站,比如个人网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构.性能的要求都很简单,随着互联网业务的不断丰富,网站 ...

  8. 「LibreOJ β Round」ZQC 的手办

    https://loj.ac/problem/504 一类套路题. 首先这个玩意可以两个logn树套树做.... naive地,把区间内的所有数拿出来放进堆里.不断取出. 太多了. 所以开始只保留那初 ...

  9. JSON的android应用实例

    JSON的android应用实例 Json在线解析器 下面是直接通过JUnit来测试直接通过API来解析Json数据 1.普通键值对象 2.Json数组对象 3.Json数组对象

  10. SSAS MDX语句 期末查询简单示例

    WITH Member [Measures].[num Last Day of Month] AS( [时间].[YQMD].CurrentMember.LastChild,[Measures].[门 ...