Skip to content

Commit 9284ece

Browse files
committed
some leetcode problems
1 parent 8fc07c0 commit 9284ece

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

.clang-format

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
---
22
BasedOnStyle: Chromium
33
IndentWidth: 4
4+
ColumnLimit: 200

leetcode/1415.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
string ans;
7+
int i = 0, n, k;
8+
9+
void helper(string s) {
10+
if (s.size() == n) {
11+
if (i == k)
12+
return;
13+
ans = s;
14+
++i;
15+
return;
16+
}
17+
if (s.empty()) {
18+
helper(s + 'a');
19+
helper(s + 'b');
20+
helper(s + 'c');
21+
} else {
22+
char c1, c2;
23+
int last = s.size() - 1;
24+
if (s[last] == 'a')
25+
c1 = 'b', c2 = 'c';
26+
else if (s[last] == 'b')
27+
c1 = 'a', c2 = 'c';
28+
else
29+
c1 = 'a', c2 = 'b';
30+
31+
helper(s + c1);
32+
helper(s + c2);
33+
}
34+
}
35+
string getHappyString(int n, int k) {
36+
this->n = n;
37+
this->k = k;
38+
helper("");
39+
if (k > i)
40+
return "";
41+
return ans;
42+
}
43+
};

leetcode/2349.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class NumberContainers {
5+
public:
6+
map<int, set<int>> num_to_indexes;
7+
map<int, int> index_to_num;
8+
NumberContainers() {
9+
/*
10+
map<int, set<int>>, key = number, val = index
11+
map<int, int>, key = index, val = number
12+
*/
13+
}
14+
15+
void change(int index, int number) {
16+
if (index_to_num[index] == 0) {
17+
index_to_num[index] = number;
18+
if (num_to_indexes.find(number) == num_to_indexes.end()) {
19+
num_to_indexes[number] = set<int>({index});
20+
} else {
21+
num_to_indexes[number].insert(index);
22+
}
23+
} else {
24+
int prev = index_to_num[index];
25+
num_to_indexes[prev].erase(num_to_indexes[prev].find(index));
26+
index_to_num[index] = number;
27+
num_to_indexes[number].insert(index);
28+
}
29+
}
30+
31+
int find(int number) {
32+
int get = *(num_to_indexes[number].begin());
33+
return get == 0 ? -1 : get;
34+
}
35+
};

leetcode/2516.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
int takeCharacters(string s, int k) {
7+
// pick an index to be a set number of right minutes, get the char count by doing suffix subtraction for a, b, c.
8+
// binary search the prefix sums for a, b, c to get corresponding left minutes indicies, take the max of that.
9+
// take the max over all possible right minute indexes [0, n - 1]
10+
// runs in O(n lg n)
11+
int n = s.size();
12+
vector<vector<int>> pfx(3, vector<int>(n + 1)), sfx(3, vector<int>(n + 1));
13+
for (int i = 0; i < 3; i++) {
14+
for (int j = 1; j <= n; j++) {
15+
pfx[i][j] = pfx[i][j - 1] + (s[j - 1] - 'a' == i);
16+
}
17+
}
18+
for (int i = 0; i < 3; i++) {
19+
for (int j = n - 1; j >= 0; j--) {
20+
sfx[i][j] = sfx[i][j + 1] + (s[j] - 'a' == i);
21+
}
22+
}
23+
24+
int ans = INT_MAX;
25+
vector<int> L(3);
26+
for (int r = 0; r <= n; r++) {
27+
for (int i = 0; i < 3; i++) {
28+
L[i] = lower_bound(pfx[i].begin(), pfx[i].end(), k - sfx[i][r]) - pfx[i].begin();
29+
}
30+
ans = min(ans, *max_element(L.begin(), L.end()) + n - r);
31+
}
32+
33+
return ans <= n ? ans : -1;
34+
}
35+
};

leetcode/2563.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
long long countFairPairs(vector<int>& nums, int lower, int upper) {
7+
long long n = nums.size(), ans = 0;
8+
sort(nums.begin(), nums.end());
9+
for (int i = 0; i < n; i++) {
10+
auto j = upper_bound(nums.begin() + i + 1, nums.end(), upper - nums[i]);
11+
auto k = lower_bound(nums.begin() + i + 1, nums.end(), lower - nums[i]);
12+
ans += j - k;
13+
}
14+
return ans;
15+
}
16+
};

0 commit comments

Comments
 (0)