Skip to content

Commit 86a4c21

Browse files
committed
Adds questions till 5th Jan
1 parent 0c2a423 commit 86a4c21

File tree

15 files changed

+512
-0
lines changed

15 files changed

+512
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Maximum Score After Splitting a String
2+
3+
**Question ID**: 1422
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 0 ms
7+
**Memory**: 8.9 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/maximum-score-after-splitting-a-string
12+
class Solution {
13+
public:
14+
int maxScore(string s) {
15+
16+
int zero=0,one=0;
17+
18+
for(auto ch : s)
19+
if(ch=='0') zero++;
20+
else one++;
21+
22+
int ans=0;
23+
24+
int cntZero=0,cntOne=0;
25+
26+
for(int i=0;i<s.size()-1;i++){
27+
char ch=s[i];
28+
if(ch=='0'){
29+
ans=max(ans,++cntZero+(one-cntOne));
30+
}else{
31+
32+
ans=max(ans,cntZero+(one- (++cntOne)));
33+
}
34+
}
35+
36+
return ans;
37+
}
38+
};
39+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1493792171,
3+
"question_id": 1422,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1738760487,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "0 ms",
10+
"url": "https://leetcode.com/submissions/detail/1493792171/",
11+
"is_pending": "Not Pending",
12+
"title": "Maximum Score After Splitting a String",
13+
"memory": "8.9 MB",
14+
"title_slug": "maximum-score-after-splitting-a-string",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// https://leetcode.com/problems/maximum-score-after-splitting-a-string
5+
6+
class Solution
7+
{
8+
public:
9+
int maxScore(string s)
10+
{
11+
12+
int zero = 0, one = 0;
13+
14+
for (auto ch : s)
15+
if (ch == '0')
16+
zero++;
17+
else
18+
one++;
19+
20+
int ans = 0;
21+
22+
int cntZero = 0, cntOne = 0;
23+
24+
for (int i = 0; i < s.size() - 1; i++)
25+
{
26+
char ch = s[i];
27+
if (ch == '0')
28+
{
29+
ans = max(ans, ++cntZero + (one - cntOne));
30+
}
31+
else
32+
{
33+
34+
ans = max(ans, cntZero + (one - (++cntOne)));
35+
}
36+
}
37+
38+
return ans;
39+
}
40+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Unique Length-3 Palindromic Subsequences
2+
3+
**Question ID**: 1930
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 177 ms
7+
**Memory**: 15.98 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/unique-length-3-palindromic-subsequences
12+
class Solution {
13+
public:
14+
int countPalindromicSubsequence(string s) {
15+
unordered_set<char> letters;
16+
for (char c : s) {
17+
letters.insert(c);
18+
}
19+
20+
int ans = 0;
21+
for (char letter : letters) {
22+
int i = -1;
23+
int j = 0;
24+
25+
for (int k = 0; k < s.size(); k++) {
26+
if (s[k] == letter) {
27+
if (i == -1) {
28+
i = k;
29+
}
30+
31+
j = k;
32+
}
33+
}
34+
35+
unordered_set<char> between;
36+
for (int k = i + 1; k < j; k++) {
37+
between.insert(s[k]);
38+
}
39+
40+
ans += between.size();
41+
}
42+
43+
return ans;
44+
}
45+
};
46+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1497730140,
3+
"question_id": 1930,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1738761016,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "177 ms",
10+
"url": "https://leetcode.com/submissions/detail/1497730140/",
11+
"is_pending": "Not Pending",
12+
"title": "Unique Length-3 Palindromic Subsequences",
13+
"memory": "15.98 MB",
14+
"title_slug": "unique-length-3-palindromic-subsequences",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
//https://leetcode.com/problems/unique-length-3-palindromic-subsequences
5+
6+
class Solution {
7+
public:
8+
int countPalindromicSubsequence(string s) {
9+
unordered_set<char> letters;
10+
for (char c : s) {
11+
letters.insert(c);
12+
}
13+
14+
int ans = 0;
15+
for (char letter : letters) {
16+
int i = -1;
17+
int j = 0;
18+
19+
for (int k = 0; k < s.size(); k++) {
20+
if (s[k] == letter) {
21+
if (i == -1) {
22+
i = k;
23+
}
24+
25+
j = k;
26+
}
27+
}
28+
29+
unordered_set<char> between;
30+
for (int k = i + 1; k < j; k++) {
31+
between.insert(s[k]);
32+
}
33+
34+
ans += between.size();
35+
}
36+
37+
return ans;
38+
}
39+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Number of Ways to Split Array
2+
3+
**Question ID**: 2270
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 0 ms
7+
**Memory**: 104.08 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/number-of-ways-to-split-array
12+
class Solution {
13+
public:
14+
int waysToSplitArray(vector<int>& nums) {
15+
16+
int n=nums.size();
17+
vector<long long> prefix(n),suffix(n);
18+
19+
prefix[0]=nums[0];
20+
suffix[n-1]=nums[n-1];
21+
22+
for(int i=1;i<n;i++){
23+
prefix[i]+=prefix[i-1]+nums[i];
24+
}
25+
26+
for(int i=n-2;i>=0;i--){
27+
suffix[i]+=suffix[i+1]+nums[i];
28+
}
29+
int cnt=0;
30+
31+
for(int i=0;i<n-1;i++){
32+
if(prefix[i]>=suffix[i+1])
33+
cnt++;
34+
}
35+
36+
return cnt;
37+
}
38+
};
39+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1496654976,
3+
"question_id": 2270,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1738760839,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "0 ms",
10+
"url": "https://leetcode.com/submissions/detail/1496654976/",
11+
"is_pending": "Not Pending",
12+
"title": "Number of Ways to Split Array",
13+
"memory": "104.08 MB",
14+
"title_slug": "number-of-ways-to-split-array",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
//https://leetcode.com/problems/number-of-ways-to-split-array
5+
6+
class Solution {
7+
public:
8+
int waysToSplitArray(vector<int>& nums) {
9+
10+
int n=nums.size();
11+
vector<long long> prefix(n),suffix(n);
12+
13+
prefix[0]=nums[0];
14+
suffix[n-1]=nums[n-1];
15+
16+
for(int i=1;i<n;i++){
17+
prefix[i]+=prefix[i-1]+nums[i];
18+
}
19+
20+
for(int i=n-2;i>=0;i--){
21+
suffix[i]+=suffix[i+1]+nums[i];
22+
}
23+
int cnt=0;
24+
25+
for(int i=0;i<n-1;i++){
26+
if(prefix[i]>=suffix[i+1])
27+
cnt++;
28+
}
29+
30+
return cnt;
31+
}
32+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Shifting Letters II
2+
3+
**Question ID**: 2381
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 49 ms
7+
**Memory**: 108.32 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/shifting-letters-ii
12+
class Solution {
13+
public:
14+
string shiftingLetters(string s, vector<vector<int>> &shifts)
15+
{
16+
vector<int> arr;
17+
18+
arr.resize(s.size(), 0);
19+
20+
for (auto i : shifts)
21+
{
22+
int start = i[0];
23+
int end = i[1];
24+
int dir = i[2] == 1 ? 1 : -1;
25+
26+
arr[start] += dir;
27+
if (end < arr.size() - 1)
28+
arr[end + 1] += -dir;
29+
}
30+
31+
for (int i = 1; i < arr.size(); i++)
32+
{
33+
arr[i] += arr[i - 1];
34+
}
35+
36+
for (int i = 0; i < s.size(); i++)
37+
{
38+
char ch = s[i];
39+
int netShift = (arr[i] % 26 + 26) % 26;
40+
s[i] = (char)('a' + (s[i] - 'a' + netShift + 26) % 26);
41+
}
42+
43+
return s;
44+
}
45+
};
46+
```

0 commit comments

Comments
 (0)