-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path948.cpp
43 lines (43 loc) · 1.33 KB
/
948.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
__________________________________________________________________________________________________
sample 8 ms submission
class Solution {
public:
int bagOfTokensScore(vector<int>& tokens, int P) {
sort(tokens.begin(), tokens.end());
int res = 0, s = 0, i = 0, j = tokens.size() - 1;
while (i <= j) {
if (P >= tokens[i]) {
P -= tokens[i++];
res = max(res, ++s);
} else if (s > 0) {
s--;
P += tokens[j--];
} else {
break;
}
}
return res;
}
};
__________________________________________________________________________________________________
sample 9484 kb submission
class Solution {
public:
int bagOfTokensScore(vector<int>& tokens, int P) {
sort(tokens.begin(), tokens.end());
int res = 0, points = 0, i = 0, j = tokens.size() - 1;
while (i <= j) {
if (P >= tokens[i]) {
P -= tokens[i++];
res = max(res, ++points);
} else if (points > 0) {
points--;
P += tokens[j--];
} else {
break;
}
}
return res;
}
};
__________________________________________________________________________________________________