Untitled
unknown
c_cpp
2 years ago
876 B
14
Indexable
class Solution {
public:
int bagOfTokensScore(vector<int>& tokens, int power) {
sort(tokens.begin(), tokens.end());
int leastInd=0;
int mostInd=tokens.size()-1;
int score=0;
while(leastInd<tokens.size() && tokens[leastInd]<= power){
score++;
power-= tokens[leastInd];
leastInd++;
}
if(score==0) return 0;
while(leastInd<=mostInd){
if(tokens[leastInd]<=tokens[mostInd]){
power+= (tokens[mostInd]- tokens[leastInd]);
leastInd++;
mostInd--;
}
while(leastInd<=mostInd && tokens[leastInd]<= power){
score++;
power-= tokens[leastInd];
leastInd++;
}
}
return score;
}
};Editor is loading...
Leave a Comment