[LeetCode] 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.

Constraints:
0 <= tokens.length <= 1000
0 <= tokens[i], power < 104

令牌放置。

给你一堆令牌,规则是令牌上tokens[i] 代表一个能量值P,每个令牌最多使用一次,使用的方法如下
如果你至少有 token[i] 点能量,可以将令牌置为正面朝上,失去 token[i] 点能量,并得到 1 分。
如果我们至少有 1 分,可以将令牌置为反面朝上,获得 token[i] 点能量,并失去 1 分。
在使用任意数量的令牌后,返回我们可以得到的最大分数。

思路

思路是排序 + 双指针。注意题目,因为无论 token 的值是多少,得分都是 1;当你需要用分数兑换能量的时候,每次只需要消耗一个分数,但是得到的 token 数量是可大可小的。由此想到需要对 tokens 数组排序。

将 input 排序,这样 token 值最小的会在数组最左边,token 值最大的会在最右边。设置两个指针从两边往中间逼近,如果当前你的能量 power >= tokens[left],说明你有多余的能量,可以拿来换分数;如果不满足这个条件,则需要看看手里目前分数是否 > 0,来换取一个当前最大的能量 power(在 right 指针那里);如果这两个条件都不满足则退出 while 循环,返回你的分数。

复杂度

时间O(nlogn) - sort
空间O(1)

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int bagOfTokensScore(int[] tokens, int power) {
Arrays.sort(tokens);
int left = 0;
int right = tokens.length - 1;
int res = 0;
int score = 0;
while (left <= right) {
if (power >= tokens[left]) {
power -= tokens[left++];
score++;
res = Math.max(res, score);
} else if (score >= 1) {
power += tokens[right--];
score--;
} else {
break;
}
}
return res;
}
}

[LeetCode] 948. Bag of Tokens
https://shurui91.github.io/posts/3870056761.html
Author
Aaron Liu
Posted on
October 26, 2020
Licensed under