Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Last active December 4, 2018 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/a18db75dcd55975f300da7664f947177 to your computer and use it in GitHub Desktop.
Save fpdjsns/a18db75dcd55975f300da7664f947177 to your computer and use it in GitHub Desktop.
[leetcode] 948. Bag of Tokens : https://leetcode.com/problems/bag-of-tokens/
class Solution {
public:
int bagOfTokensScore(vector<int>& tokens, int P) {
sort(tokens.begin(),tokens.end());
int r = tokens.size() - 1;
int ans = 0;
int point = 0;
for(int i=0; i<min((int)tokens.size(), r + 1); i++){
int now = tokens[i];
if(now > P && 0 < point){
point--;
P += tokens[r--];
}
if(now > P)
break;
point++;
P -= now;
ans = max(ans, point);
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment