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)


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