Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
876 B
1
Indexable
Never
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;

        
    }
};
Leave a Comment