diff --git a/solution/0900-0999/0948.Bag of Tokens/README.md b/solution/0900-0999/0948.Bag of Tokens/README.md index d3eb51ee73f01..96d19dc593044 100644 --- a/solution/0900-0999/0948.Bag of Tokens/README.md +++ b/solution/0900-0999/0948.Bag of Tokens/README.md @@ -85,7 +85,7 @@ tags: 因此,我们可以将令牌按照消耗能量的多少进行排序,然后使用双指针,一个指针从左向右遍历,一个指针从右向左遍历,每次遍历都尽可能地消耗能量得到分数,然后更新最大分数。如果当前能量不足以消耗当前令牌,那么我们就尝试使用分数来消耗当前令牌,如果分数不足以消耗当前令牌,那么我们就停止遍历。 -时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 为令牌的数量。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是令牌的数量。 @@ -95,16 +95,16 @@ tags: class Solution: def bagOfTokensScore(self, tokens: List[int], power: int) -> int: tokens.sort() + ans = score = 0 i, j = 0, len(tokens) - 1 - ans = t = 0 while i <= j: if power >= tokens[i]: power -= tokens[i] - i, t = i + 1, t + 1 - ans = max(ans, t) - elif t: + score, i = score + 1, i + 1 + ans = max(ans, score) + elif score: power += tokens[j] - j, t = j - 1, t - 1 + score, j = score - 1, j - 1 else: break return ans @@ -116,16 +116,14 @@ class Solution: class Solution { public int bagOfTokensScore(int[] tokens, int power) { Arrays.sort(tokens); - int i = 0, j = tokens.length - 1; - int ans = 0, t = 0; - while (i <= j) { + int ans = 0, score = 0; + for (int i = 0, j = tokens.length - 1; i <= j;) { if (power >= tokens[i]) { power -= tokens[i++]; - ++t; - ans = Math.max(ans, t); - } else if (t > 0) { + ans = Math.max(ans, ++score); + } else if (score > 0) { power += tokens[j--]; - --t; + --score; } else { break; } @@ -142,15 +140,14 @@ class Solution { public: int bagOfTokensScore(vector& tokens, int power) { sort(tokens.begin(), tokens.end()); - int i = 0, j = tokens.size() - 1; - int ans = 0, t = 0; - while (i <= j) { + int ans = 0, score = 0; + for (int i = 0, j = tokens.size() - 1; i <= j;) { if (power >= tokens[i]) { power -= tokens[i++]; - ans = max(ans, ++t); - } else if (t) { + ans = max(ans, ++score); + } else if (score > 0) { power += tokens[j--]; - --t; + --score; } else { break; } @@ -163,23 +160,47 @@ public: #### Go ```go -func bagOfTokensScore(tokens []int, power int) int { +func bagOfTokensScore(tokens []int, power int) (ans int) { sort.Ints(tokens) i, j := 0, len(tokens)-1 - ans, t := 0, 0 + score := 0 for i <= j { if power >= tokens[i] { power -= tokens[i] - i, t = i+1, t+1 - ans = max(ans, t) - } else if t > 0 { + i++ + score++ + ans = max(ans, score) + } else if score > 0 { power += tokens[j] - j, t = j-1, t-1 + j-- + score-- } else { break } } - return ans + return +} +``` + +#### TypeScript + +```ts +function bagOfTokensScore(tokens: number[], power: number): number { + tokens.sort((a, b) => a - b); + let [i, j] = [0, tokens.length - 1]; + let [ans, score] = [0, 0]; + while (i <= j) { + if (power >= tokens[i]) { + power -= tokens[i++]; + ans = Math.max(ans, ++score); + } else if (score) { + power += tokens[j--]; + score--; + } else { + break; + } + } + return ans; } ``` diff --git a/solution/0900-0999/0948.Bag of Tokens/README_EN.md b/solution/0900-0999/0948.Bag of Tokens/README_EN.md index 24ddd7e9ea43a..27640b5692817 100644 --- a/solution/0900-0999/0948.Bag of Tokens/README_EN.md +++ b/solution/0900-0999/0948.Bag of Tokens/README_EN.md @@ -131,7 +131,13 @@ tags: -### Solution 1 +### Solution 1: Greedy + Sorting + Two Pointers + +There are two ways to use tokens: one is to consume energy to gain points, and the other is to consume points to gain energy. Obviously, we should consume as little energy as possible to gain as many points as possible. + +Therefore, we can sort the tokens by the amount of energy they consume, and then use two pointers: one moving from left to right and the other from right to left. In each iteration, we try to consume energy to gain points as much as possible, and then update the maximum score. If the current energy is not enough to consume the current token, we try to consume the current token using points. If the points are not enough to consume the current token, we stop the iteration. + +The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of tokens. @@ -141,16 +147,16 @@ tags: class Solution: def bagOfTokensScore(self, tokens: List[int], power: int) -> int: tokens.sort() + ans = score = 0 i, j = 0, len(tokens) - 1 - ans = t = 0 while i <= j: if power >= tokens[i]: power -= tokens[i] - i, t = i + 1, t + 1 - ans = max(ans, t) - elif t: + score, i = score + 1, i + 1 + ans = max(ans, score) + elif score: power += tokens[j] - j, t = j - 1, t - 1 + score, j = score - 1, j - 1 else: break return ans @@ -162,16 +168,14 @@ class Solution: class Solution { public int bagOfTokensScore(int[] tokens, int power) { Arrays.sort(tokens); - int i = 0, j = tokens.length - 1; - int ans = 0, t = 0; - while (i <= j) { + int ans = 0, score = 0; + for (int i = 0, j = tokens.length - 1; i <= j;) { if (power >= tokens[i]) { power -= tokens[i++]; - ++t; - ans = Math.max(ans, t); - } else if (t > 0) { + ans = Math.max(ans, ++score); + } else if (score > 0) { power += tokens[j--]; - --t; + --score; } else { break; } @@ -188,15 +192,14 @@ class Solution { public: int bagOfTokensScore(vector& tokens, int power) { sort(tokens.begin(), tokens.end()); - int i = 0, j = tokens.size() - 1; - int ans = 0, t = 0; - while (i <= j) { + int ans = 0, score = 0; + for (int i = 0, j = tokens.size() - 1; i <= j;) { if (power >= tokens[i]) { power -= tokens[i++]; - ans = max(ans, ++t); - } else if (t) { + ans = max(ans, ++score); + } else if (score > 0) { power += tokens[j--]; - --t; + --score; } else { break; } @@ -209,23 +212,47 @@ public: #### Go ```go -func bagOfTokensScore(tokens []int, power int) int { +func bagOfTokensScore(tokens []int, power int) (ans int) { sort.Ints(tokens) i, j := 0, len(tokens)-1 - ans, t := 0, 0 + score := 0 for i <= j { if power >= tokens[i] { power -= tokens[i] - i, t = i+1, t+1 - ans = max(ans, t) - } else if t > 0 { + i++ + score++ + ans = max(ans, score) + } else if score > 0 { power += tokens[j] - j, t = j-1, t-1 + j-- + score-- } else { break } } - return ans + return +} +``` + +#### TypeScript + +```ts +function bagOfTokensScore(tokens: number[], power: number): number { + tokens.sort((a, b) => a - b); + let [i, j] = [0, tokens.length - 1]; + let [ans, score] = [0, 0]; + while (i <= j) { + if (power >= tokens[i]) { + power -= tokens[i++]; + ans = Math.max(ans, ++score); + } else if (score) { + power += tokens[j--]; + score--; + } else { + break; + } + } + return ans; } ``` diff --git a/solution/0900-0999/0948.Bag of Tokens/Solution.cpp b/solution/0900-0999/0948.Bag of Tokens/Solution.cpp index 479ff7b4895ef..ff642f24ff12c 100644 --- a/solution/0900-0999/0948.Bag of Tokens/Solution.cpp +++ b/solution/0900-0999/0948.Bag of Tokens/Solution.cpp @@ -2,15 +2,14 @@ class Solution { public: int bagOfTokensScore(vector& tokens, int power) { sort(tokens.begin(), tokens.end()); - int i = 0, j = tokens.size() - 1; - int ans = 0, t = 0; - while (i <= j) { + int ans = 0, score = 0; + for (int i = 0, j = tokens.size() - 1; i <= j;) { if (power >= tokens[i]) { power -= tokens[i++]; - ans = max(ans, ++t); - } else if (t) { + ans = max(ans, ++score); + } else if (score > 0) { power += tokens[j--]; - --t; + --score; } else { break; } diff --git a/solution/0900-0999/0948.Bag of Tokens/Solution.go b/solution/0900-0999/0948.Bag of Tokens/Solution.go index 3a699daa3b5d3..fa67e759711aa 100644 --- a/solution/0900-0999/0948.Bag of Tokens/Solution.go +++ b/solution/0900-0999/0948.Bag of Tokens/Solution.go @@ -1,18 +1,20 @@ -func bagOfTokensScore(tokens []int, power int) int { +func bagOfTokensScore(tokens []int, power int) (ans int) { sort.Ints(tokens) i, j := 0, len(tokens)-1 - ans, t := 0, 0 + score := 0 for i <= j { if power >= tokens[i] { power -= tokens[i] - i, t = i+1, t+1 - ans = max(ans, t) - } else if t > 0 { + i++ + score++ + ans = max(ans, score) + } else if score > 0 { power += tokens[j] - j, t = j-1, t-1 + j-- + score-- } else { break } } - return ans + return } \ No newline at end of file diff --git a/solution/0900-0999/0948.Bag of Tokens/Solution.java b/solution/0900-0999/0948.Bag of Tokens/Solution.java index b03f5c96bf09f..3a5fe8fd9629e 100644 --- a/solution/0900-0999/0948.Bag of Tokens/Solution.java +++ b/solution/0900-0999/0948.Bag of Tokens/Solution.java @@ -1,16 +1,14 @@ class Solution { public int bagOfTokensScore(int[] tokens, int power) { Arrays.sort(tokens); - int i = 0, j = tokens.length - 1; - int ans = 0, t = 0; - while (i <= j) { + int ans = 0, score = 0; + for (int i = 0, j = tokens.length - 1; i <= j;) { if (power >= tokens[i]) { power -= tokens[i++]; - ++t; - ans = Math.max(ans, t); - } else if (t > 0) { + ans = Math.max(ans, ++score); + } else if (score > 0) { power += tokens[j--]; - --t; + --score; } else { break; } diff --git a/solution/0900-0999/0948.Bag of Tokens/Solution.py b/solution/0900-0999/0948.Bag of Tokens/Solution.py index 85ea4f759c86d..af91e37cb927d 100644 --- a/solution/0900-0999/0948.Bag of Tokens/Solution.py +++ b/solution/0900-0999/0948.Bag of Tokens/Solution.py @@ -1,16 +1,16 @@ class Solution: def bagOfTokensScore(self, tokens: List[int], power: int) -> int: tokens.sort() + ans = score = 0 i, j = 0, len(tokens) - 1 - ans = t = 0 while i <= j: if power >= tokens[i]: power -= tokens[i] - i, t = i + 1, t + 1 - ans = max(ans, t) - elif t: + score, i = score + 1, i + 1 + ans = max(ans, score) + elif score: power += tokens[j] - j, t = j - 1, t - 1 + score, j = score - 1, j - 1 else: break return ans diff --git a/solution/0900-0999/0948.Bag of Tokens/Solution.ts b/solution/0900-0999/0948.Bag of Tokens/Solution.ts new file mode 100644 index 0000000000000..27159d810a8fd --- /dev/null +++ b/solution/0900-0999/0948.Bag of Tokens/Solution.ts @@ -0,0 +1,17 @@ +function bagOfTokensScore(tokens: number[], power: number): number { + tokens.sort((a, b) => a - b); + let [i, j] = [0, tokens.length - 1]; + let [ans, score] = [0, 0]; + while (i <= j) { + if (power >= tokens[i]) { + power -= tokens[i++]; + ans = Math.max(ans, ++score); + } else if (score) { + power += tokens[j--]; + score--; + } else { + break; + } + } + return ans; +}