-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path948.cpp
More file actions
26 lines (26 loc) · 701 Bytes
/
948.cpp
File metadata and controls
26 lines (26 loc) · 701 Bytes
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
class Solution {
public:
int bagOfTokensScore(vector<int>& tokens, int power) {
sort(tokens.begin(), tokens.end());
int left = 0;
int right = tokens.size() - 1;
int n = tokens.size();
int score = 0;
int res = 0;
while (left <= right) {
while (left < n && tokens[left] <= power) {
power -= tokens[left];
score++;
left++;
}
res = max(res, score);
if (left <= right && score > 0) {
score--;
power += tokens[right];
right--;
}
else break;
}
return res;
}
};