948. 令牌放置 - 力扣(LeetCode) (leetcode-cn.com)
我发现我还挺厉害~~
class Solution {
public:
int bagOfTokensScore(vector<int>& tokens, int power) {
sort(tokens.begin(),tokens.end());
int p=power;
int s=0;
int temp=tokens.size()-1;
for(int i=0;i<=temp;i++){
if(p>=tokens[i]){
cout<<tokens[i]<<" "<<p<<" "<<s<<endl;
s++;
p-=tokens[i];
}
else{
if(s==0) return 0;
else{
s--;
p+=tokens[temp];
temp--;
cout<<tokens[i]<<" "<<p<<" "<<s<<endl;
if(p>=tokens[i]){
s++;
p-=tokens[i];
}
else return s;
}
}
}
return s;
}
};