题目如下:

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. HTTP协议-Cookie和Session详解

    前言: 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的跟踪技术就是Cookie和Session. Cookie通过在客户端记录信息确定用户身份,Session通过在 ...

  2. 常见HTTP错误代码

    了解更多HTTP错误代码 一些常见的状态码为: 200 - 服务器成功返回网页404 - 请求的网页不存在503 - 服务不可用详细分解: 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态 ...

  3. flutter页面布局三

    RaisedButton 为了实现今天的效果,在认识Wrap组件之前,先认识一下flutter中的按钮组件,Flutter 中通过 RaisedButton 定义一个按钮. import 'packa ...

  4. react教程 — 组件的生命周期 和 执行顺序

    一.组件执行的生命周期:                  参考  https://www.cnblogs.com/soyxiaobi/p/9559117.html  或  https://www.c ...

  5. 自动收集有效IP代理

    自动收集有效IP代理 #需要的外部依赖包requests和lxml#自动获取的代理ip数据保存为”IP代理池.txt“#read_ip函数用于提取”IP代理池.txt“的数据返回类型为列表from l ...

  6. git filter-branch

    https://github.com/git-for-windows/git/issues/2206 https://git-scm.com/docs/git-filter-branch The -- ...

  7. STL排序函数

    Qsort,Sort,Stable_sort,Partial_sort,List::sort 参考

  8. appium常见问题07_appium输入中文无效

    前几天在appium android自动化测试过程中,使用send_keys()输入中文,发现只能输入字母和数字,输入中文无反应. 大家是否同样遇到过该问题,当大家同样遇到该问题时,在配置参数desi ...

  9. Html5 学习笔记 --》css3 学习

    在开发任务中最好不要使用前缀 可以设置发散图形 圆形 方形等 边框图片效果: CSS3 变形效果: Css3 3D立体变形: css 设置 CSS3 过度效果: div:hover { backgro ...

  10. C# 几进制 转换到几进制

    public string ConvertString(string value, int fromBase, int toBase) { int intValue = Convert.ToInt32 ...