add leetcode content

This commit is contained in:
gameloader 2024-03-04 11:42:56 +08:00
parent 7ff717e4c2
commit ed1e385216

View File

@ -422,3 +422,99 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
} }
``` ```
## day7 2024-03-04
### 948. Bag of Tokens
You start with an initial power of power, an initial score of 0, and a bag of tokens given as an integer array tokens, where each tokens[i] donates the value of tokeni.
Your goal is to maximize the total score by strategically playing these tokens. In one move, you can play an unplayed token in one of the two ways (but not both for the same token):
**Face-up**: If your current power is at least tokens[i], you may play tokeni, losing tokens[i] power and gaining 1 score.
**Face-down**: If your current score is at least 1, you may play tokeni, gaining tokens[i] power and losing 1 score.
Return the maximum possible score you can achieve after playing any number of tokens.
Example 1:
Input: tokens = [100], power = 50
Output: 0
Explanation: Since your score is 0 initially, you cannot play the token face-down. You also cannot play it face-up since your power (50) is less than tokens[0] (100).
Example 2:
Input: tokens = [200,100], power = 150
Output: 1
Explanation: Play token1 (100) face-up, reducing your power to 50 and increasing your score to 1.
There is no need to play token0, since you cannot play it face-up to add to your score. The maximum score achievable is 1.
Example 3:
Input: tokens = [100,200,300,400], power = 200
Output: 2
Explanation: Play the tokens in this order to get a score of 2:
Play token0 (100) face-up, reducing power to 100 and increasing score to 1.
Play token3 (400) face-down, increasing power to 500 and reducing score to 0.
Play token1 (200) face-up, reducing power to 300 and increasing score to 1.
Play token2 (300) face-up, reducing power to 0 and increasing score to 2.
The maximum score achievable is 2.
### 题解
本题的目的是最大化score. 一个基本的策略就是通过小的power换取score, 通过score换取大的power, 利用换到的大power赚取中间水平的token的score. 关键在于, 如何找到最大能换取的score. 首先考虑每次进行一次Face-up和Face-down, score没有变化, 只有power增大了, 那么每次都将score置0, 并判断当前能获得的最大score即可.
通过前面的分析可以得出, 算法分为以下几步
1. 将tokens数组排序
2. 判断power是否大于tokens[0], 即最小的token, 若大于, 则计算当前能获得的最大score
3. 将power的值增加目前tokens数组中最大值和最小值(即排好序后的最后一项和第一项)的差值, 同时将tokens数组中第一项和最后一项移除. 重复2, 3步直到power小于tokens[0]或tokens数组长度为0中止.
4. 返回最大的score
### 代码
```go
func bagOfTokensScore(tokens []int, power int) int {
sort.Ints(tokens)
var powernormal []int
remain := power
score := 0
powernormal = append(powernormal, score)
for len(tokens) != 0 && power > tokens[0]{
remain = power
for _, value := range tokens{
if power >= value{
score++
power -= value
} else{
break
}
}
powernormal = append(powernormal, score)
score = 0
remain += tokens[len(tokens)-1] - tokens[0]
if len(tokens) <= 1{
break
}
tokens = tokens[1:len(tokens)-1]
power = remain
}
sort.Ints(powernormal)
return powernormal[len(powernormal)-1]
}
```
### 总结
在实现过程中, 起初使用一个数组来保存每次的score值, 这样空间复杂度略大, 后来查看他人代码, 发现只需要一个max变量来保存当前最大的score值, 并在每次循环计算当前轮次的score值时与当前的最大值比较并根据二者大小更新max变量的值即可, 这样只需要O(1)的空间复杂度.