[Swift]LeetCode948. 令牌放置 | Bag of Tokens

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-fopgfgpx-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

You have an initial power P, an initial score of 0 points, and a bag of tokens.git

Each token can be used at most once, has a value token[i], and has potentially two ways to use it.github

  • 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 1point.

Return the largest number of points we can have after playing any number of tokens.微信

Example 1:spa

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

Example 2:code

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

Example 3:htm

Input: tokens = [100,200,300,400], P = 200 Output: 2 

 Note:blog

  1. tokens.length <= 1000
  2. 0 <= tokens[i] < 10000
  3. 0 <= P < 10000

你的初始能量爲 P,初始分數爲 0,只有一包令牌。token

令牌的值爲 token[i],每一個令牌最多隻能使用一次,可能的兩種使用方法以下:rem

  • 若是你至少有 token[i] 點能量,能夠將令牌置爲正面朝上,失去 token[i] 點能量,並獲得 1 分。
  • 若是咱們至少有 1 分,能夠將令牌置爲反面朝上,得到 token[i] 點能量,並失去 1 分。

在使用任意數量的令牌後,返回咱們能夠獲得的最大分數。

示例 1:

輸入:tokens = [100], P = 50
輸出:0

示例 2:

輸入:tokens = [100,200], P = 150
輸出:1

示例 3:

輸入:tokens = [100,200,300,400], P = 200
輸出:2

提示:

  1. tokens.length <= 1000
  2. 0 <= tokens[i] < 10000
  3. 0 <= P < 10000

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 }
相關文章
相關標籤/搜索