forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0948.cpp
More file actions
28 lines (27 loc) · 653 Bytes
/
0948.cpp
File metadata and controls
28 lines (27 loc) · 653 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
27
28
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int bagOfTokensScore(vector<int>& tokens, int P)
{
int res = 0, cur = 0, i = 0, j = tokens.size()-1;
sort(tokens.begin(), tokens.end());
while (i <= j and cur >= 0)
{
if (tokens[i] <= P)
{
P -= tokens[i++];
res = max(res, ++cur);
}
else
{
P += tokens[j--];
cur--;
}
}
return res;
}
};