Skip to content

Commit

Permalink
Solution to today's problem
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Mar 4, 2024
1 parent efc7299 commit 9bb112e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ public int bagOfTokensScore(int[] _tokens, int _power) {
Arrays.sort(_tokens);
int i = 0;
int j = _tokens.length - 1;
int ans = 0;
int result = 0;
int t = 0;
while (i <= j) {
if (_power >= _tokens[i]) {
_power -= _tokens[i++];
t++;
ans = Math.max(ans, t);
result = Math.max(result, t);
} else if (t > 0) {
_power += _tokens[j--];
t--;
} else {
break;
}
}
return ans;
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.spannm.leetcode.lc0.lc0900;

import io.github.spannm.leetcode.LeetcodeBaseTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.CsvSource;

class Problem0948Test extends LeetcodeBaseTest {

@ParameterizedTest(name = "[{index}] {0}; {1} --> {2}")
@CsvSource(delimiter = ';', value = {
"100; 50; 0",
"200,100; 150; 1",
"100,200,300,400; 200; 2"

})
void test(@ConvertWith(CsvToIntArray.class) int[] _tokens, int _power, int _expectedResult) {
assertEquals(_expectedResult, new Problem0948().bagOfTokensScore(_tokens, _power));
}

}

0 comments on commit 9bb112e

Please sign in to comment.