Skip to content

Commit 8077f35

Browse files
Update 0948-bag-of-tokens.java
1 parent 72b8d84 commit 8077f35

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

java/0948-bag-of-tokens.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,27 @@ public int bagOfTokensScore(int[] tokens, int power) {
2222
return res;
2323
}
2424
}
25+
26+
/*------------------------------------------------------*/
27+
28+
class Solution {
29+
public int bagOfTokensScore(int[] tokens, int power) {
30+
Arrays.sort(tokens);
31+
int score = 0;
32+
int i = 0, j = tokens.length - 1;
33+
while (i < j) {
34+
if (power >= tokens[i]) {
35+
power -= tokens[i++];
36+
score += 1;
37+
}
38+
else if (score > 0 && i != j) {
39+
score -= 1;
40+
power += tokens[j--];
41+
}
42+
else
43+
break;
44+
}
45+
return score;
46+
}
47+
}
48+

0 commit comments

Comments
 (0)