Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 32224f6

Browse files
commit
1 parent f81e747 commit 32224f6

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <string>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <utility>
5+
6+
using namespace std;
7+
8+
class Solution {
9+
public:
10+
int maximumGain(string s, int x, int y) {
11+
int totalPoints = 0;
12+
string high_priority_pair = (x > y) ? "ab" : "ba";
13+
string low_priority_pair = (x > y) ? "ba" : "ab";
14+
int high_priority_score = max(x, y);
15+
int low_priority_score = min(x, y);
16+
17+
// First pass: remove high priority pairs
18+
pair<int, string> after_first_pass = solve(s, high_priority_pair, high_priority_score);
19+
totalPoints += after_first_pass.first;
20+
21+
// Second pass: remove low priority pairs from the remaining string
22+
pair<int, string> after_second_pass = solve(after_first_pass.second, low_priority_pair, low_priority_score);
23+
totalPoints += after_second_pass.first;
24+
25+
return totalPoints;
26+
}
27+
28+
private:
29+
// Helper function to process the string and remove a specific pattern
30+
pair<int, string> solve(const string& text, const string& pattern, int score) {
31+
string stack;
32+
int points = 0;
33+
for (char c : text) {
34+
if (!stack.empty() && stack.back() == pattern[0] && c == pattern[1]) {
35+
stack.pop_back();
36+
points += score;
37+
} else {
38+
stack.push_back(c);
39+
}
40+
}
41+
return {points, stack};
42+
}
43+
};

‎455. Assign Cookies.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int findContentChildren(vector<int>& g, vector<int>& s) {
4+
5+
int ptr_g = 0 , ptr_s = 0;
6+
int count = 0;
7+
while( ptr_s < s.size() && ptr_g<g.size() ){
8+
9+
if( s[ptr_s] >= g[ptr_g] ){
10+
count++;
11+
ptr_g++;
12+
}
13+
14+
ptr_s++;
15+
}
16+
17+
return count;
18+
}
19+
};

‎485. Max Consecutive Ones.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findMaxConsecutiveOnes(vector<int>& nums) {
4+
int count1 = 0;
5+
int max_count = 0;
6+
7+
for (int i = 0; i < nums.size(); i++) {
8+
if (nums[i] == 1) {
9+
count1++;
10+
max_count = max(max_count, count1);
11+
} else {
12+
count1 = 0; // reset if 0 is found
13+
}
14+
}
15+
16+
return max_count;
17+
}
18+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /