leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0948.cpp (496B)


0 class Solution { 1 public: 2 int bagOfTokensScore(vector<int> &tokens, int power) const { 3 sort(begin(tokens), end(tokens)); 4 5 const int n = size(tokens); 6 int score = 0, i = 0, j = n - 1; 7 while (i <= j) { 8 if (power >= tokens[i]) 9 score++, power -= tokens[i++]; 10 else if (i < j && score > 0) 11 score--, power += tokens[j--]; 12 else 13 return score; 14 } 15 return score; 16 } 17 };