Skip to content

Commit 7d1bd16

Browse files
🛄 Day 24
1 parent 0994d0a commit 7d1bd16

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
1. [Minimum Depth of Binary Tree](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/562/week-4-october-22nd-october-28th/3504/) ➡️ [CPP Solution](Week4/minDepth.cpp)
4141
2. [132 Pattern](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/562/week-4-october-22nd-october-28th/3505/) ➡️ [CPP Solution](Week4/find132pattern.cpp)
42+
3. [Bag of Tokens](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/562/week-4-october-22nd-october-28th/3506/) ➡️ [CPP Solution](Week4/bagOfTokensScore.cpp)
4243

4344
## Week 5 🚧
4445

Week4/bagOfTokensScore.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int bagOfTokensScore(vector<int>& tokens, int P) {
4+
int n = tokens.size();
5+
if(n == 0) return 0;
6+
7+
sort(tokens.begin(), tokens.end());
8+
9+
int i = 0, j = n - 1, score = 0, maxScore = 0;
10+
11+
while(i <= j) {
12+
if(tokens[i] <= P) {
13+
P -= tokens[i++];
14+
score++;
15+
maxScore = max(maxScore, score);
16+
} else if(score > 0) {
17+
P += tokens[j--];
18+
score--;
19+
} else {
20+
return maxScore;
21+
}
22+
}
23+
24+
return maxScore;
25+
}
26+
};

0 commit comments

Comments
 (0)