-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path948.cpp
More file actions
26 lines (25 loc) · 727 Bytes
/
948.cpp
File metadata and controls
26 lines (25 loc) · 727 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
// Daily 30.3.2024
// 948. Bag of Tokens
// https://leetcode.com/problems/bag-of-tokens/submissions/1218315921/
class Solution {
public:
int bagOfTokensScore(vector<int>& tokens, int power) {
sort(tokens.begin(), tokens.end());
int score = 0;
int highScore = score;
int left = 0, right = tokens.size() -1;
while(left <= right){
if(power >= tokens[left]){
power -= tokens[left++];
score++;
highScore = max(score, highScore);
}
else{
power += tokens[right--];
score--;
if(score < 0 ) return 0;
}
}
return highScore;
}
};