![[Swift Weekly Contest 112]LeetCode948. 令牌放置 | Bag of Tokens,第1张 [Swift Weekly Contest 112]LeetCode948. 令牌放置 | Bag of Tokens,第1张](/aiimages/%5BSwift+Weekly+Contest+112%5DLeetCode948.+%E4%BB%A4%E7%89%8C%E6%94%BE%E7%BD%AE+%7C+Bag+of+Tokens.png)
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.
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 1point. 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:
tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000 你的初始能量为 P,初始分数为 0,只有一包令牌。
令牌的值为 token[i],每个令牌最多只能使用一次,可能的两种使用方法如下:
token[i] 点能量,可以将令牌置为正面朝上,失去 token[i] 点能量,并得到 1 分。 如果我们至少有 1 分,可以将令牌置为反面朝上,获得 token[i] 点能量,并失去 1 分。 在使用任意数量的令牌后,返回我们可以得到的最大分数。
示例 1:
输入:tokens = [100],P = 50输出:0
示例 2:
输入:tokens = [100,P = 150输出:1
示例 3:
输入:tokens = [100,P = 200输出:2
提示:
tokens.length <= 1000 0 <= tokens[i] < 10000 0 <= P < 10000 76ms 1 class Solution { 2 func bagOfTokensscore(_ tokens: [Int],_ P: Int) -> Int { 3 var tokens = tokens.sorted(by:<) 4 var P = P 5 if tokens.count == 0 || P < tokens[0] 6 { 7 return 0 8 } 9 var n:Int = tokens.count10 var p:Int = 011 var point:Int = 012 var ret:Int = 013 for i in 0...n14 {15 if i > 016 {17 P += tokens[n-i]18 point -= 119 }20 while(p < n-i && P >= tokens[p])21 {22 P -= tokens[p]23 point += 124 p += 125 }26 if p <= n-i27 {28 ret = max(ret,point)29 }30 }31 return ret32 }33 }总结
以上是内存溢出为你收集整理的[Swift Weekly Contest 112]LeetCode948. 令牌放置 | Bag of Tokens全部内容,希望文章能够帮你解决[Swift Weekly Contest 112]LeetCode948. 令牌放置 | Bag of Tokens所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)