Skip to content

Commit a5634ba

Browse files
authored
feat: add solutions to lc problem: No.0948 (doocs#3296)
1 parent 9951bdf commit a5634ba

File tree

7 files changed

+141
-77
lines changed

7 files changed

+141
-77
lines changed

solution/0900-0999/0948.Bag of Tokens/README.md

+47-26
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ tags:
8585

8686
因此,我们可以将令牌按照消耗能量的多少进行排序,然后使用双指针,一个指针从左向右遍历,一个指针从右向左遍历,每次遍历都尽可能地消耗能量得到分数,然后更新最大分数。如果当前能量不足以消耗当前令牌,那么我们就尝试使用分数来消耗当前令牌,如果分数不足以消耗当前令牌,那么我们就停止遍历。
8787

88-
时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 为令牌的数量
88+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是令牌的数量
8989

9090
<!-- tabs:start -->
9191

@@ -95,16 +95,16 @@ tags:
9595
class Solution:
9696
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
9797
tokens.sort()
98+
ans = score = 0
9899
i, j = 0, len(tokens) - 1
99-
ans = t = 0
100100
while i <= j:
101101
if power >= tokens[i]:
102102
power -= tokens[i]
103-
i, t = i + 1, t + 1
104-
ans = max(ans, t)
105-
elif t:
103+
score, i = score + 1, i + 1
104+
ans = max(ans, score)
105+
elif score:
106106
power += tokens[j]
107-
j, t = j - 1, t - 1
107+
score, j = score - 1, j - 1
108108
else:
109109
break
110110
return ans
@@ -116,16 +116,14 @@ class Solution:
116116
class Solution {
117117
public int bagOfTokensScore(int[] tokens, int power) {
118118
Arrays.sort(tokens);
119-
int i = 0, j = tokens.length - 1;
120-
int ans = 0, t = 0;
121-
while (i <= j) {
119+
int ans = 0, score = 0;
120+
for (int i = 0, j = tokens.length - 1; i <= j;) {
122121
if (power >= tokens[i]) {
123122
power -= tokens[i++];
124-
++t;
125-
ans = Math.max(ans, t);
126-
} else if (t > 0) {
123+
ans = Math.max(ans, ++score);
124+
} else if (score > 0) {
127125
power += tokens[j--];
128-
--t;
126+
--score;
129127
} else {
130128
break;
131129
}
@@ -142,15 +140,14 @@ class Solution {
142140
public:
143141
int bagOfTokensScore(vector<int>& tokens, int power) {
144142
sort(tokens.begin(), tokens.end());
145-
int i = 0, j = tokens.size() - 1;
146-
int ans = 0, t = 0;
147-
while (i <= j) {
143+
int ans = 0, score = 0;
144+
for (int i = 0, j = tokens.size() - 1; i <= j;) {
148145
if (power >= tokens[i]) {
149146
power -= tokens[i++];
150-
ans = max(ans, ++t);
151-
} else if (t) {
147+
ans = max(ans, ++score);
148+
} else if (score > 0) {
152149
power += tokens[j--];
153-
--t;
150+
--score;
154151
} else {
155152
break;
156153
}
@@ -163,23 +160,47 @@ public:
163160
#### Go
164161
165162
```go
166-
func bagOfTokensScore(tokens []int, power int) int {
163+
func bagOfTokensScore(tokens []int, power int) (ans int) {
167164
sort.Ints(tokens)
168165
i, j := 0, len(tokens)-1
169-
ans, t := 0, 0
166+
score := 0
170167
for i <= j {
171168
if power >= tokens[i] {
172169
power -= tokens[i]
173-
i, t = i+1, t+1
174-
ans = max(ans, t)
175-
} else if t > 0 {
170+
i++
171+
score++
172+
ans = max(ans, score)
173+
} else if score > 0 {
176174
power += tokens[j]
177-
j, t = j-1, t-1
175+
j--
176+
score--
178177
} else {
179178
break
180179
}
181180
}
182-
return ans
181+
return
182+
}
183+
```
184+
185+
#### TypeScript
186+
187+
```ts
188+
function bagOfTokensScore(tokens: number[], power: number): number {
189+
tokens.sort((a, b) => a - b);
190+
let [i, j] = [0, tokens.length - 1];
191+
let [ans, score] = [0, 0];
192+
while (i <= j) {
193+
if (power >= tokens[i]) {
194+
power -= tokens[i++];
195+
ans = Math.max(ans, ++score);
196+
} else if (score) {
197+
power += tokens[j--];
198+
score--;
199+
} else {
200+
break;
201+
}
202+
}
203+
return ans;
183204
}
184205
```
185206

solution/0900-0999/0948.Bag of Tokens/README_EN.md

+53-26
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,13 @@ tags:
131131

132132
<!-- solution:start -->
133133

134-
### Solution 1
134+
### Solution 1: Greedy + Sorting + Two Pointers
135+
136+
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.
137+
138+
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.
139+
140+
The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of tokens.
135141

136142
<!-- tabs:start -->
137143

@@ -141,16 +147,16 @@ tags:
141147
class Solution:
142148
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
143149
tokens.sort()
150+
ans = score = 0
144151
i, j = 0, len(tokens) - 1
145-
ans = t = 0
146152
while i <= j:
147153
if power >= tokens[i]:
148154
power -= tokens[i]
149-
i, t = i + 1, t + 1
150-
ans = max(ans, t)
151-
elif t:
155+
score, i = score + 1, i + 1
156+
ans = max(ans, score)
157+
elif score:
152158
power += tokens[j]
153-
j, t = j - 1, t - 1
159+
score, j = score - 1, j - 1
154160
else:
155161
break
156162
return ans
@@ -162,16 +168,14 @@ class Solution:
162168
class Solution {
163169
public int bagOfTokensScore(int[] tokens, int power) {
164170
Arrays.sort(tokens);
165-
int i = 0, j = tokens.length - 1;
166-
int ans = 0, t = 0;
167-
while (i <= j) {
171+
int ans = 0, score = 0;
172+
for (int i = 0, j = tokens.length - 1; i <= j;) {
168173
if (power >= tokens[i]) {
169174
power -= tokens[i++];
170-
++t;
171-
ans = Math.max(ans, t);
172-
} else if (t > 0) {
175+
ans = Math.max(ans, ++score);
176+
} else if (score > 0) {
173177
power += tokens[j--];
174-
--t;
178+
--score;
175179
} else {
176180
break;
177181
}
@@ -188,15 +192,14 @@ class Solution {
188192
public:
189193
int bagOfTokensScore(vector<int>& tokens, int power) {
190194
sort(tokens.begin(), tokens.end());
191-
int i = 0, j = tokens.size() - 1;
192-
int ans = 0, t = 0;
193-
while (i <= j) {
195+
int ans = 0, score = 0;
196+
for (int i = 0, j = tokens.size() - 1; i <= j;) {
194197
if (power >= tokens[i]) {
195198
power -= tokens[i++];
196-
ans = max(ans, ++t);
197-
} else if (t) {
199+
ans = max(ans, ++score);
200+
} else if (score > 0) {
198201
power += tokens[j--];
199-
--t;
202+
--score;
200203
} else {
201204
break;
202205
}
@@ -209,23 +212,47 @@ public:
209212
#### Go
210213
211214
```go
212-
func bagOfTokensScore(tokens []int, power int) int {
215+
func bagOfTokensScore(tokens []int, power int) (ans int) {
213216
sort.Ints(tokens)
214217
i, j := 0, len(tokens)-1
215-
ans, t := 0, 0
218+
score := 0
216219
for i <= j {
217220
if power >= tokens[i] {
218221
power -= tokens[i]
219-
i, t = i+1, t+1
220-
ans = max(ans, t)
221-
} else if t > 0 {
222+
i++
223+
score++
224+
ans = max(ans, score)
225+
} else if score > 0 {
222226
power += tokens[j]
223-
j, t = j-1, t-1
227+
j--
228+
score--
224229
} else {
225230
break
226231
}
227232
}
228-
return ans
233+
return
234+
}
235+
```
236+
237+
#### TypeScript
238+
239+
```ts
240+
function bagOfTokensScore(tokens: number[], power: number): number {
241+
tokens.sort((a, b) => a - b);
242+
let [i, j] = [0, tokens.length - 1];
243+
let [ans, score] = [0, 0];
244+
while (i <= j) {
245+
if (power >= tokens[i]) {
246+
power -= tokens[i++];
247+
ans = Math.max(ans, ++score);
248+
} else if (score) {
249+
power += tokens[j--];
250+
score--;
251+
} else {
252+
break;
253+
}
254+
}
255+
return ans;
229256
}
230257
```
231258

solution/0900-0999/0948.Bag of Tokens/Solution.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ class Solution {
22
public:
33
int bagOfTokensScore(vector<int>& tokens, int power) {
44
sort(tokens.begin(), tokens.end());
5-
int i = 0, j = tokens.size() - 1;
6-
int ans = 0, t = 0;
7-
while (i <= j) {
5+
int ans = 0, score = 0;
6+
for (int i = 0, j = tokens.size() - 1; i <= j;) {
87
if (power >= tokens[i]) {
98
power -= tokens[i++];
10-
ans = max(ans, ++t);
11-
} else if (t) {
9+
ans = max(ans, ++score);
10+
} else if (score > 0) {
1211
power += tokens[j--];
13-
--t;
12+
--score;
1413
} else {
1514
break;
1615
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
func bagOfTokensScore(tokens []int, power int) int {
1+
func bagOfTokensScore(tokens []int, power int) (ans int) {
22
sort.Ints(tokens)
33
i, j := 0, len(tokens)-1
4-
ans, t := 0, 0
4+
score := 0
55
for i <= j {
66
if power >= tokens[i] {
77
power -= tokens[i]
8-
i, t = i+1, t+1
9-
ans = max(ans, t)
10-
} else if t > 0 {
8+
i++
9+
score++
10+
ans = max(ans, score)
11+
} else if score > 0 {
1112
power += tokens[j]
12-
j, t = j-1, t-1
13+
j--
14+
score--
1315
} else {
1416
break
1517
}
1618
}
17-
return ans
19+
return
1820
}

solution/0900-0999/0948.Bag of Tokens/Solution.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
class Solution {
22
public int bagOfTokensScore(int[] tokens, int power) {
33
Arrays.sort(tokens);
4-
int i = 0, j = tokens.length - 1;
5-
int ans = 0, t = 0;
6-
while (i <= j) {
4+
int ans = 0, score = 0;
5+
for (int i = 0, j = tokens.length - 1; i <= j;) {
76
if (power >= tokens[i]) {
87
power -= tokens[i++];
9-
++t;
10-
ans = Math.max(ans, t);
11-
} else if (t > 0) {
8+
ans = Math.max(ans, ++score);
9+
} else if (score > 0) {
1210
power += tokens[j--];
13-
--t;
11+
--score;
1412
} else {
1513
break;
1614
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution:
22
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
33
tokens.sort()
4+
ans = score = 0
45
i, j = 0, len(tokens) - 1
5-
ans = t = 0
66
while i <= j:
77
if power >= tokens[i]:
88
power -= tokens[i]
9-
i, t = i + 1, t + 1
10-
ans = max(ans, t)
11-
elif t:
9+
score, i = score + 1, i + 1
10+
ans = max(ans, score)
11+
elif score:
1212
power += tokens[j]
13-
j, t = j - 1, t - 1
13+
score, j = score - 1, j - 1
1414
else:
1515
break
1616
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function bagOfTokensScore(tokens: number[], power: number): number {
2+
tokens.sort((a, b) => a - b);
3+
let [i, j] = [0, tokens.length - 1];
4+
let [ans, score] = [0, 0];
5+
while (i <= j) {
6+
if (power >= tokens[i]) {
7+
power -= tokens[i++];
8+
ans = Math.max(ans, ++score);
9+
} else if (score) {
10+
power += tokens[j--];
11+
score--;
12+
} else {
13+
break;
14+
}
15+
}
16+
return ans;
17+
}

0 commit comments

Comments
 (0)