https://leetcode.com/problems/bag-of-tokens/
Approach 1: Greedy
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.- If we have at least
token[i]
power, we may play the token face up, losingtoken[i]
power, and gaining1
point. - If we have at least
1
point, we may play the token face down, gainingtoken[i]
power, and losing1
point.
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
If we play a token face up, we might as well play the one with the smallest value. Similarly, if we play a token face down, we might as well play the one with the largest value.
Algorithm
We don't need to play anything until absolutely necessary. Let's play tokens face up until we can't, then play a token face down and continue.
We must be careful, as it is easy for our implementation to be incorrect if we do not handle corner cases correctly. We should always play tokens face up until exhaustion, then play one token face down and continue.
Our loop must be constructed with the right termination condition: we can either play a token face up or face down.
Our final answer could be any of the intermediate answers we got after playing tokens face up (but before playing them face down.)
- Time Complexity: , where is the length of
tokens
. - Space Complexity: .
public int bagOfTokensScore(int[] tokens, int P) {
Arrays.sort(tokens);
int lo = 0, hi = tokens.length - 1;
int points = 0, ans = 0;
while (lo <= hi && (P >= tokens[lo] || points > 0)) {
while (lo <= hi && P >= tokens[lo]) {
P -= tokens[lo++];
points++;
}
ans = Math.max(ans, points);
if (lo <= hi && points > 0) {
P += tokens[hi--];
points--;
}
}
return ans;
}
public int bagOfTokensScore(int[] tokens, int point) {
Arrays.sort(tokens);
ArrayDeque<Integer> queue = new ArrayDeque<>(Arrays.stream(tokens).boxed().collect(Collectors.toList()));
int score = 0;
while (queue.size() > 1) {
// without changed, forever loop
boolean changed = false;
// bugs: missing !queue.isEmpty(), >= not >
while (!queue.isEmpty() && point >= queue.peekFirst()) {
score++;
point -= queue.removeFirst();
changed = true;
}
if (score > 0 && queue.size() > 1) {
score--;
point += queue.removeLast();
changed = true;
}
if (!changed)
break;
}
if (!queue.isEmpty() && point > queue.peekFirst()) {
score++;
}
return score;
}
https://leetcode.com/problems/bag-of-tokens/discuss/197696/C%2B%2BJavaPython-Greedy-%2B-Two-Pointers
Sort tokens.
Buy at the cheapest and sell at the most expensive.
Buy at the cheapest and sell at the most expensive.
public int bagOfTokensScore(int[] tokens, int P) {
Arrays.sort(tokens);
int res = 0, points = 0, i = 0, j = tokens.length - 1;
while (i <= j) {
if (P >= tokens[i]) {
P -= tokens[i++];
res = Math.max(res, ++points);
} else if (points > 0) {
points--;
P += tokens[j--];
} else {
break;
}
}
return res;
}
https://leetcode.com/problems/bag-of-tokens/discuss/197669/Python-two-pointers-O(N*logN)-time-O(1)-space