作者: 负雪明烛
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:

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

Example 2:

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

Example 3:

  1. Input: tokens = [100,200,300,400], P = 200
  2. 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的。

  1. class Solution(object):
  2. def bagOfTokensScore(self, tokens, P):
  3. """
  4. :type tokens: List[int]
  5. :type P: int
  6. :rtype: int
  7. """
  8. print(tokens)
  9. tokens.sort()
  10. N = len(tokens)
  11. left, right = 0, N - 1
  12. points = 0
  13. remain = N
  14. while left < N and P >= tokens[left]:
  15. P -= tokens[left]
  16. points += 1
  17. left += 1
  18. remain -= 1
  19. if left == 0 or left == N: return points
  20. while points > 0 and remain > 1:
  21. P += tokens[right]
  22. right -= 1
  23. points -= 1
  24. remain -= 1
  25. while left <= right and P >= tokens[left]:
  26. P -= tokens[left]
  27. points += 1
  28. left += 1
  29. remain -= 1
  30. 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. CQC认证与3C认证的区别是什么

    中国CCC认证为中国的强制性认证(什么产品能做,CQC网上都要目录), CQC是自愿性的,(不能做CCC认证的产品,可以选择做CQC认证) 产品想要在中国市场上流通,特别是进入超市,商场,招标工厂等的 ...

  2. Linux-centos7设置静态IP地址

    参考:https://blog.csdn.net/sjhuangx/article/details/79618865

  3. 编程艺术第十六~第二十章:全排列/跳台阶/奇偶调序,及一致性Hash算法

    目录(?)[+]   第十六~第二十章:全排列,跳台阶,奇偶排序,第一个只出现一次等问题 作者:July.2011.10.16.出处:http://blog.csdn.net/v_JULY_v. 引言 ...

  4. 【MPI环境配置】 vs2019配置MPI环境

    MPI 即 Message-Passing Interface,提供了一系列并行编程的接口,为了在本机能够学习和使用并行编程,需要提前安装MPI; 配置环境: Microsoft Visual Stu ...

  5. Android中的性能优化

    由于手机硬件的限制,内存和CPU都无法像pc一样具有超大的内存,Android手机上,过多的使用内存,会容易导致oom,过多的使用CPU资源,会导致手机卡顿,甚至导致anr.我主要是从一下几部分进行优 ...

  6. mybatis-plus解析

    mybatis-plus当用lambda时bean属性不要以is/get/set开头,解析根据字段而不是get/set方法映射

  7. Largest Rectangle in Histogram及二维解法

    昨天看岛娘直播解题,看到很经典的一题Largest Rectangle in Histogram 题目地址:https://leetcode.com/problems/largest-rectangl ...

  8. 4.1 python中调用rust程序

    概述 使用rust-cpython将rust程序做为python模块调用: 通常为了提高python的性能: 参考 https://github.com/dgrunwald/rust-cpython ...

  9. ORACLE 按逗号拆分字符串为多行

    with t as (select '1,2,3,10,11,12' a from dual) select substr(a, decode(level - 1, 0, 0, instr(a, ', ...

  10. 【Linux】【Services】【Package】编译安装

    程序包编译安装:         testapp-VERSION-release.src.rpm --> 安装后,使用rpmbuild命令制作成二进制格式的rpm包,而后再安装:         ...