-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBag_of_Tokens.java
More file actions
38 lines (38 loc) · 957 Bytes
/
Bag_of_Tokens.java
File metadata and controls
38 lines (38 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
import java.io.*;
import java.lang.*;
public class Bag_of_Tokens {
class Solution {
public int bagOfTokensScore(int[] tokens, int power) {
Arrays.sort(tokens);
int l = 0;
int r = tokens.length - 1;
int score = 0;
int max = 0;
while(l<=r)
{
if(power>=tokens[l])
{
score++;
power = power - tokens[l];
l++;
if(max < score)
{
max = score;
}
}
else if(score>0)
{
power = power + tokens[r];
score--;
r--;
}
else
{
break;
}
}
return max;
}
}
}