Skip to content

Commit

Permalink
Time: 62 ms (81.25%), Space: 42.4 MB (37.50%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalCodeRicardo committed Mar 4, 2024
1 parent 8268632 commit 57370e8
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 0948-bag-of-tokens/0948-bag-of-tokens.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
public class Solution
{

public int BagOfTokensScore(int[] tokens, int power)
{
var max = 0;
var l = 0;
var r = tokens.Length -1;
var s = 0;
Array.Sort(tokens);
while(l<=r) {
if (power>= tokens[l]) {
power = power - tokens[l];
l++;
s++;

if (max < s) {
max = s;
}

} else if (s >0) {
power = power + tokens[r];
r--;
s--;
} else {
break;
}


}

return max;
}


}

0 comments on commit 57370e8

Please sign in to comment.