作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/bag-of-tokens/description/

题目描述

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

题目大意

有个power,现在有两种操作:

  1. 当power超过token[i]的时候,可以把token[i]进行翻转成正面,然后得到了1个点;
  2. 当至少有1个点的时候,可以把任何一个token[i]进行翻转成反面,然后丢失1个点。

可以认为token在刚开始的时候既不是正面也不是反面。

问最后能获得的最多的点数是多少。

解题方法

贪心算法

使用了两个指针,左边指向翻成正面的token,右边指向翻成反面的token.

首先,对token先排序。

第一步,我们用现在所有的power把左边的都翻成正面,同时获得了一些点。这一步之后,我们使用power贪心地获得了所有的点数。

第二步,我们看右边能翻转多少个反面,这个能获得Power,所以我们使用的点数换取了更多的power.

这两步来回走的话,就能获得更多的Points。

需要注意的是,如果剩余的token只有一个的时候,我们是不把它换成power的。

class Solution(object):
def bagOfTokensScore(self, tokens, P):
"""
:type tokens: List[int]
:type P: int
:rtype: int
"""
print(tokens)
tokens.sort()
N = len(tokens)
left, right = 0, N - 1
points = 0
remain = N
while left < N and P >= tokens[left]:
P -= tokens[left]
points += 1
left += 1
remain -= 1
if left == 0 or left == N: return points
while points > 0 and remain > 1:
P += tokens[right]
right -= 1
points -= 1
remain -= 1
while left <= right and P >= tokens[left]:
P -= tokens[left]
points += 1
left += 1
remain -= 1
return points

日期

2018 年 11 月 24 日 —— 周日开始!一周就过去了~

【LeetCode】948. Bag of Tokens 解题报告(Python)的更多相关文章

  1. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  2. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  4. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  5. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  6. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  7. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  8. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

随机推荐

  1. Linux文件系统属性和权限概念详解(包含inode、block、文件权限、文件软硬链接等)

    Linux中的文件属性 ls -lih 包括:索引节点(inode),文件类型,权限属性,硬链接数,所归属的用户和用户组,文件大小,最近修改时间,文件名等等 索引节点:相当于身份证号,系统唯一,系统读 ...

  2. 移动测试(web和app)及app测试实战

    移动测试androidiosapp上 原生GUI 混合应用H5 web端兼容性浏览器测试需要的内容:safari 浏览器edge浏览器ie11浏览器firefox浏览器chrome浏览器 国内360浏 ...

  3. accent, accept

    accent A colon (:) is used to represent a long vowel, e.g. sheet /ʃiːt/ and shit /ʃit/. The word bed ...

  4. flink04 -----1 kafkaSource 2. kafkaSource的偏移量的存储位置 3 将kafka中的数据写入redis中去 4 将kafka中的数据写入mysql中去

    1. kafkaSource 见官方文档 2. kafkaSource的偏移量的存储位置 默认存在kafka的特殊topic中,但也可以设置参数让其不存在kafka的特殊topic中   3   将k ...

  5. pyqt5 的串口编写进度

    2020.12.18 今天遇到一个问题, 想用回车实现串口数据的发送. 下面这句话是让光标移动到文字的尾部,但是不能够实现. 对QTextEdit控件中的文字改写,或清除后,再调用下面的移动到尾部,就 ...

  6. Java中特殊的类——包装类

    Java中特殊的类--包装类 包装类就是将基本数据类型封装在类中. 1.包装类 (1)自定义包装类 将基本数据类型包装成一个类对象的本质就是使用Object进行接收处理. 此时IntDemo类就是in ...

  7. Can namespaces be nested in C++?

    In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example ...

  8. go goroutines 使用小结

    go +方法 就实现了一个并发,但由于环境不同,需要对并发的个数进行限制,限制同一时刻并发的个数,后面称此为"并发限流". 为什么要并发限流? 虽然GO M+P+G的方式号称可以轻 ...

  9. java面试--(生成随机数,获取重复次数最多,并且数是最大的一个,打印出来)

    import java.util.*; public class MaxRandom { public static void main(String[] args){ int[] num = new ...

  10. pipeline groovy

    目录 一.变量 一.变量 1.直接定义 def x="abc" 2.从脚本执行结果赋值变量 branch = "/jen_script/return-branch.sh ...