在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ You have an initial power Each token can be used at most once, has a value
Return the largest number of points we can have after playing any number of tokens. Example 1: Input: tokens = 50
Output: 0
Example 2: Input: tokens = 150
Output: 1
Example 3: Input: tokens = 200
Output: 2
Note:
你的初始能量为 令牌的值为
在使用任意数量的令牌后,返回我们可以得到的最大分数。 示例 1: 输入:tokens = [100], P = 50 输出:0 示例 2: 输入:tokens = [100,200], P = 150 输出:1 示例 3: 输入:tokens = [100,200,300,400], P = 200 输出:2 提示:
40ms
1 class Solution { 2 func bagOfTokensScore(_ tokens: [Int], _ P: Int) -> Int { 3 var curTokens = tokens 4 var curP = P 5 curTokens.sort{$0<$1} 6 var lo = 0; 7 var hi = curTokens.count-1; 8 var points = 0 9 var ans = 0 10 while (lo <= hi && (curP >= curTokens[lo] || points > 0)){ 11 while (lo <= hi && curP >= curTokens[lo]){ 12 curP -= curTokens[lo] 13 lo += 1 14 points += 1; 15 } 16 ans = ans > points ? ans : points 17 if(lo <= hi && points > 0){ 18 curP += curTokens[hi] 19 hi -= 1 20 points -= 1 21 } 22 } 23 24 return ans 25 } 26 } 44ms 1 class Solution { 2 func bagOfTokensScore(_ tokens: [Int], _ P: Int) -> Int { 3 guard !tokens.isEmpty else { return 0 } 4 let tokens = tokens.sorted() 5 6 var l = 0 7 var h = tokens.count - 1 8 var result = 0 9 var current = 0 10 var power = P 11 12 while l <= h { 13 let cToken = tokens[l] 14 if power >= cToken { 15 current += 1 16 result = max(result, current) 17 power -= cToken 18 l += 1 19 } else if current > 0 { 20 current -= 1 21 power += tokens[h] 22 h -= 1 23 } else { 24 break 25 } 26 } 27 28 return result 29 } 72ms 1 class Solution { 2 3 func bagOfTokensScore(_ tokens: [Int], _ power: Int) -> Int { 4 let tokens = tokens.sorted() 5 var power = power 6 7 var leftIndex = 0 8 var rightIndex = tokens.count - 1 9 var points = 0 10 var maxPoints = 0 11 while leftIndex <= rightIndex { 12 if power >= tokens[leftIndex] { 13 power -= tokens[leftIndex] 14 leftIndex += 1 15 points += 1 16 maxPoints = max(maxPoints, points) 17 } else if points > 0 { 18 power += tokens[rightIndex] 19 rightIndex -= 1 20 points -= 1 21 } else { 22 break 23 } 24 } 25 26 return maxPoints 27 } 28 } 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.count 10 var p:Int = 0 11 var point:Int = 0 12 var ret:Int = 0 13 for i in 0...n 14 { 15 if i > 0 16 { 17 P += tokens[n-i] 18 point -= 1 19 } 20 while(p < n-i && P >= tokens[p]) 21 { 22 P -= tokens[p] 23 point += 1 24 p += 1 25 } 26 if p <= n-i 27 { 28 ret = max(ret, point) 29 } 30 } 31 return ret 32 } 33 } 144ms 1 class Solution { 2 func bagOfTokensScore(_ tokens: [Int], _ P: Int) -> Int { 3 4 if tokens.count == 0 { 5 return 0 6 } 7 8 var sortedTokens = tokens.sorted() 9 10 if sortedTokens[0] > P { 11 return 0 12 } 13 14 var maxScore = 0 15 var score = 0 16 var currentPower = P 17 18 while !sortedTokens.isEmpty { 19 let firstToken = sortedTokens.first! 20 print("firstToken \(firstToken) currentPower \(currentPower)") 21 if firstToken <= currentPower { 22 score += 1 23 maxScore = max(maxScore, score) 24 currentPower -= sortedTokens.removeFirst() 25 } else { 26 score -= 1 27 currentPower += sortedTokens.removeLast() 28 print("currentPower \(currentPower)") 29 } 30 } 31 32 return maxScore 33 } 34 }
|
请发表评论