Last active
December 4, 2018 10:28
-
-
Save fpdjsns/a18db75dcd55975f300da7664f947177 to your computer and use it in GitHub Desktop.
[leetcode] 948. Bag of Tokens : https://leetcode.com/problems/bag-of-tokens/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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