aboutsummaryrefslogtreecommitdiff
path: root/bag_of_tokens.cpp
blob: d645d2bacb0592ac8f2a7f5caf0f6b131f27a123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "leetcode.h"

class Solution {
public:
  int bagOfTokensScore(vector<int> &tokens, int power) {
    sort(tokens.begin(), tokens.end());
    int i, j, n;
    i = 0;
    j = n = tokens.size();
    while (i < j) {
      if (power >= tokens[i])
        power -= tokens[i++];
      else if (i - (n - j) && j > i + 1)
        power += tokens[--j];
      else
        break;
    }
    return i - (n - j);
  }
};

int main() {
  Solution obj;
  vector<int> tokens = {100};
  int power = 50;
  cout << obj.bagOfTokensScore(tokens, power);
}