【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 ...
随机推荐
- Vue----渐进式框架的理解
对“渐进式”这三个字的理解:Vue渐进式-先使用Vue的核心库,再根据你的需要的功能再去逐渐增加加相应的插件. 以下理解出处:https://www.zhihu.com/question/519072 ...
- 英语单词operand
operand 来源 [root@centos7 ~]# mkdir mkdir: missing operand 缺少操作数,也就是要创建的目录 Try 'mkdir --help' for mo ...
- bzoj 2002[Hnoi2010]Bounce 弹飞绵羊(分治分块)
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- LUOGU P4609 [FJOI2016]建筑师(第一类斯特林数)
传送门 解题思路 好神仙的思路,首先一种排列中按照最高点将左右分开,那么就是要在左边选出\(a-1\)个,右边选出\(b-1\)一个,这个如何计算呢?考虑第一类斯特林数,第一类斯特林数是将\(n\)个 ...
- 牛客假日团队赛9 A 乘积最大 (简单DP)
题目:https://ac.nowcoder.com/acm/contest/1071/A 题意:给你一个串,然后给你m个乘号,用m个乘号分割开这个串,然后求分割可以求出的最大值 思路:首先范围很小 ...
- tomcat启动前端项目
前后端分离项目,前端使用vue,部署启动前端项目可以使用NodeJS,Nginx,Tomcat. *)使用Tomcat部署启动: 1.把vue项目build生成的dist包,放到Tomcat的weba ...
- 2018-2019-2 20175120 实验五《Java网络编程》实验报告
实验报告封面 课程:Java程序设计 班级:1751班 姓名:彭宇辰 学号:20175120 指导教师:娄嘉鹏 实验日期:2019年5月26日 实验时间:13:10 - 15:25 实验序号:20 实 ...
- Nginx (限速)限制并发、限制访问速率、限制流量
Nginx 限制并发访问速率流量,配置还是简单的,看下Nginx文档根据文中这三个模块对照看一下就可以,Nginx限速使用的是漏桶算法(感兴趣可以看下文末的参考资料),需要注意的是:当需要进行限速操作 ...
- 不同字符串,HashCode可能相同
不同的字符串,hashcode可能相同. 先看例子: @Test public void test6(){ System.out.println("ABCDEa123abc".ha ...
- js判断一件商品加还是减
<div class="right"> <div onclick="javascript: ClickJian(this);" data-i= ...