File tree 1 file changed +19
-19
lines changed 1 file changed +19
-19
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public int bagOfTokensScore (int [] tokens , int power ) {
3
- int score = 0 ;
4
- Arrays .sort (tokens );
5
- int leftIdx = 0 ;
6
- int rightIdx = tokens .length - 1 ;
7
- int currScore = 0 ;
8
- while (leftIdx <= rightIdx ) {
9
- if (power >= tokens [leftIdx ]) {
10
- currScore ++;
11
- power -= tokens [leftIdx ++];
12
- } else {
13
- if (currScore == 0 ) {
14
- break ;
2
+ public int bagOfTokensScore (int [] tokens , int power ) {
3
+ int score = 0 ;
4
+ int currScore = 0 ;
5
+ Arrays .sort (tokens );
6
+ int left = 0 ;
7
+ int right = tokens .length - 1 ;
8
+ while (left <= right ) {
9
+ if (power >= tokens [left ]) {
10
+ currScore ++;
11
+ power -= tokens [left ++];
12
+ } else {
13
+ if (currScore == 0 ) {
14
+ break ;
15
+ }
16
+ currScore --;
17
+ power += tokens [right --];
18
+ }
19
+ score = Math .max (score , currScore );
15
20
}
16
- currScore --;
17
- power += tokens [rightIdx --];
18
- }
19
- score = Math .max (score , currScore );
21
+ return score ;
20
22
}
21
- return score ;
22
- }
23
23
}
You can’t perform that action at this time.
0 commit comments