|
| 1 | +# 948. Bag of Tokens |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Two Pointers, Greedy, Sorting. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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 token**i**. |
| 10 | + |
| 11 | +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): |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +- **Face-up**: If your current power is **at least** `tokens[i]`, you may play token**i**, losing `tokens[i]` power and gaining `1` score. |
| 16 | + |
| 17 | +- **Face-down**: If your current score is **at least** `1`, you may play token**i**, gaining `tokens[i]` power and losing `1` score. |
| 18 | + |
| 19 | + |
| 20 | +Return **the **maximum** possible score you can achieve after playing **any** number of tokens**. |
| 21 | + |
| 22 | + |
| 23 | +Example 1: |
| 24 | + |
| 25 | + |
| 26 | +**Input:** tokens = [100], power = 50 |
| 27 | + |
| 28 | +**Output:** 0 |
| 29 | + |
| 30 | +**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`). |
| 31 | + |
| 32 | + |
| 33 | +Example 2: |
| 34 | + |
| 35 | + |
| 36 | +**Input:** tokens = [200,100], power = 150 |
| 37 | + |
| 38 | +**Output:** 1 |
| 39 | + |
| 40 | +**Explanation:** Play token**1** (`100`) face-up, reducing your power to `50` and increasing your score to `1`. |
| 41 | + |
| 42 | +There is no need to play token**0**, since you cannot play it face-up to add to your score. The maximum score achievable is `1`. |
| 43 | + |
| 44 | + |
| 45 | +Example 3: |
| 46 | + |
| 47 | + |
| 48 | +**Input:** tokens = [100,200,300,400], power = 200 |
| 49 | + |
| 50 | +**Output:** 2 |
| 51 | + |
| 52 | +**Explanation:** Play the tokens in this order to get a score of `2`: |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +- Play token**0** (`100`) face-up, reducing power to `100` and increasing score to `1`. |
| 57 | + |
| 58 | +- Play token**3** (`400`) face-down, increasing power to `500` and reducing score to `0`. |
| 59 | + |
| 60 | +- Play token**1** (`200`) face-up, reducing power to `300` and increasing score to `1`. |
| 61 | + |
| 62 | +- Play token**2** (`300`) face-up, reducing power to `0` and increasing score to `2`. |
| 63 | + |
| 64 | + |
| 65 | +The maximum score achievable is 2. |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +**Constraints:** |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +- `0 <= tokens.length <= 1000` |
| 74 | + |
| 75 | +- `0 <= tokens[i], power < 104` |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +## Solution |
| 80 | + |
| 81 | +```javascript |
| 82 | +/** |
| 83 | + * @param {number[]} tokens |
| 84 | + * @param {number} power |
| 85 | + * @return {number} |
| 86 | + */ |
| 87 | +var bagOfTokensScore = function(tokens, power) { |
| 88 | + tokens.sort((a, b) => a - b); |
| 89 | + var left = 0; |
| 90 | + var right = tokens.length - 1; |
| 91 | + var maxScore = 0; |
| 92 | + var score = 0; |
| 93 | + while (left <= right) { |
| 94 | + if (power >= tokens[left]) { |
| 95 | + power -= tokens[left++]; |
| 96 | + score += 1; |
| 97 | + maxScore = Math.max(maxScore, score); |
| 98 | + } else if (score > 0) { |
| 99 | + score -= 1; |
| 100 | + power += tokens[right--]; |
| 101 | + } else { |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + return maxScore; |
| 106 | +}; |
| 107 | +``` |
| 108 | + |
| 109 | +**Explain:** |
| 110 | + |
| 111 | +nope. |
| 112 | + |
| 113 | +**Complexity:** |
| 114 | + |
| 115 | +* Time complexity : O(n * log(n)). |
| 116 | +* Space complexity : O(1). |
0 commit comments