Skip to content

Commit 6582bc3

Browse files
committed
2299-2302 solved.
1 parent 1d7e7aa commit 6582bc3

File tree

9 files changed

+214
-0
lines changed

9 files changed

+214
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(A)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(A main.cpp)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// Source : https://leetcode.com/problems/strong-password-checker-ii/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-11
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <string>
8+
9+
using namespace std;
10+
11+
12+
/// Simulation
13+
/// Time Complexity: O(n)
14+
/// Space Complexity: O(1)
15+
class Solution {
16+
public:
17+
bool strongPasswordCheckerII(string password) {
18+
19+
if(password.size() < 8) return false;
20+
21+
bool lower_ok = false;
22+
for(char c: password) if(islower(c)) lower_ok = true;
23+
if(!lower_ok) return false;
24+
25+
bool upper_ok = false;
26+
for(char c: password) if(isupper(c)) upper_ok = true;
27+
if(!upper_ok) return false;
28+
29+
bool digit_ok = false;
30+
for(char c: password) if(isdigit(c)) digit_ok = true;
31+
if(!digit_ok) return false;
32+
33+
bool special_ok = false;
34+
string special = "!@#$%^&*()-+";
35+
for(char c: password) if(special.find(c) != string::npos) special_ok = true;
36+
if(!special_ok) return false;
37+
38+
for(int i = 1; i < password.size(); i ++)
39+
if(password[i - 1] == password[i]) return false;
40+
return true;
41+
}
42+
};
43+
44+
45+
int main() {
46+
47+
return 0;
48+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(B)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(B main.cpp)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// Source : https://leetcode.com/problems/successful-pairs-of-spells-and-potions/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-11
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
9+
using namespace std;
10+
11+
12+
/// Binary Search
13+
/// Time Complexity: O(nlogm)
14+
/// Space Complexity: O(1)
15+
class Solution {
16+
public:
17+
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
18+
19+
vector<long long> spells_ll(spells.size());
20+
for(int i = 0; i < spells.size(); i ++)
21+
spells_ll[i] = spells[i];
22+
23+
vector<long long> potions_ll(potions.size());
24+
for(int i = 0; i < potions.size(); i ++)
25+
potions_ll[i] = potions[i];
26+
sort(potions_ll.begin(), potions_ll.end());
27+
28+
vector<int> res(spells.size());
29+
for(int i = 0; i < spells.size(); i ++){
30+
long long energy = spells_ll[i];
31+
long long least_potion = success / energy;
32+
if(least_potion * energy < success) least_potion ++;
33+
34+
res[i] = potions_ll.end() - lower_bound(potions_ll.begin(), potions_ll.end(), least_potion);
35+
}
36+
return res;
37+
}
38+
};
39+
40+
41+
int main() {
42+
43+
return 0;
44+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(C)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(C main.cpp)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// Source : https://leetcode.com/problems/match-substring-after-replacement/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-11
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Using Map Table
12+
/// Time Complexity: O(n * m)
13+
/// Space Complexity: O(1)
14+
class Solution {
15+
public:
16+
bool matchReplacement(string s, string sub, vector<vector<char>>& mappings) {
17+
18+
vector<vector<bool>> map_table(256, vector<bool>(256, false));
19+
for(const vector<char>& mapping: mappings){
20+
char old_ch = mapping[0], new_ch = mapping[1];
21+
map_table[old_ch][new_ch] = true;
22+
}
23+
24+
for(int i = 0; i + sub.size() <= s.size(); i ++){
25+
26+
bool ok = true;
27+
for(int j = 0; j < sub.size() && ok; j ++)
28+
ok = sub[j] == s[i + j] || map_table[sub[j]][s[i + j]];
29+
if(ok) return true;
30+
}
31+
return false;
32+
}
33+
};
34+
35+
36+
int main() {
37+
38+
return 0;
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(D)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(D main.cpp)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/// Source : https://leetcode.com/problems/count-subarrays-with-score-less-than-k/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-11
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Sliding Window
12+
/// Time Complexity: O(n)
13+
/// Space Complexity: O(1)
14+
class Solution {
15+
public:
16+
long long countSubarrays(vector<int>& nums, long long k) {
17+
18+
int l = 0, r = -1, n = nums.size();
19+
long long cur_sum = 0, res = 0;
20+
while(l < n){
21+
if(r + 1 < n && 1ll * (cur_sum + nums[r + 1]) * (r + 1 - l + 1) < k){
22+
r ++;
23+
cur_sum += nums[r];
24+
}
25+
else{
26+
if(l <= r){
27+
res += (r - l + 1);
28+
cur_sum -= nums[l];
29+
}
30+
l ++;
31+
r = max(r, l - 1);
32+
}
33+
}
34+
return res;
35+
}
36+
};
37+
38+
39+
int main() {
40+
41+
vector<int> nums1 = {2, 1, 4, 3, 5};
42+
cout << Solution().countSubarrays(nums1, 10) << '\n';
43+
// 6
44+
45+
vector<int> nums2 = {1, 1, 1};
46+
cout << Solution().countSubarrays(nums2, 5) << '\n';
47+
// 5
48+
49+
vector<int> nums3 = {2, 1, 4, 3, 5};
50+
cout << Solution().countSubarrays(nums3, 3) << '\n';
51+
// 2
52+
53+
return 0;
54+
}

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
21422142
| 2296 | [Design a Text Editor](https://leetcode.com/problems/design-a-text-editor/) | [] | [C++](2001-2500/2296-Design-a-Text-Editor/cpp-2296/) | | |
21432143
| 2297 | [Jump Game IX](https://leetcode.com/problems/jump-game-ix/) | [] | [C++](2001-2500/2297-Jump-Game-IX/cpp-2297/) | | |
21442144
| | | | | | |
2145+
| 2299 | [Strong Password Checker II](https://leetcode.com/problems/strong-password-checker-ii/) | [] | [C++](2001-2500/2299-Strong-Password-Checker-II/cpp-2299/) | | |
2146+
| 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions/) | [] | [C++](2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/) | | |
2147+
| 2301 | [Match Substring After Replacement](https://leetcode.com/problems/match-substring-after-replacement/) | [] | [C++](2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/) | | |
2148+
| 2302 | [Count Subarrays With Score Less Than K](https://leetcode.com/problems/count-subarrays-with-score-less-than-k/) | [] | [C++](2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/) | | |
2149+
| | | | | | |
21452150

21462151
## 力扣中文站比赛
21472152

0 commit comments

Comments
 (0)