Skip to content

Commit ca3de8d

Browse files
committed
2729-2732 solved.
1 parent 6ae267f commit ca3de8d

File tree

9 files changed

+191
-0
lines changed

9 files changed

+191
-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.25)
2+
project(cpp_2729)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_2729 main.cpp)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// Source : https://leetcode.com/problems/check-if-the-number-is-fascinating/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-06-13
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <numeric>
8+
#include <algorithm>
9+
10+
using namespace std;
11+
12+
13+
/// Simulation
14+
/// Time Complexity: O(logn)
15+
/// Space Complexity: O(1)
16+
class Solution {
17+
public:
18+
bool isFascinating(int n) {
19+
20+
int a = n, b = 2 * n, c = 3 * n;
21+
string s = to_string(a) + to_string(b) + to_string(c);
22+
23+
vector<int> f(10, 0);
24+
for(char c: s) f[c - '0'] ++;
25+
return count_if(f.begin() + 1, f.end(), [](int e){return e == 1;}) == 9;
26+
}
27+
};
28+
29+
30+
int main() {
31+
32+
return 0;
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
project(cpp_2730)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_2730 main.cpp)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// Source : https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-06-13
4+
5+
#include <iostream>
6+
#include <algorithm>
7+
8+
using namespace std;
9+
10+
11+
/// Brute Force
12+
/// Time Complexity: O(n^2)
13+
/// Space Complexity: O(1)
14+
class Solution {
15+
public:
16+
int longestSemiRepetitiveSubstring(string s) {
17+
18+
int res = 1;
19+
for(int i = 0; i + 1 < s.size(); i ++)
20+
res = max(res, solve(s, i, i + 1));
21+
return res;
22+
}
23+
24+
private:
25+
int solve(const string& s, int a, int b){
26+
int res = 2;
27+
for(int i = a - 1; i >= 0 && s[i] != s[i + 1]; i --) res ++;
28+
for(int i = b + 1; i < s.size() && s[i] != s[i - 1]; i ++) res ++;
29+
return res;
30+
}
31+
};
32+
33+
34+
int main() {
35+
36+
return 0;
37+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
project(cpp_2731)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_2731 main.cpp)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// Source : https://leetcode.com/problems/movement-of-robots/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-06-13
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
9+
using namespace std;
10+
11+
12+
/// Ad-Hoc
13+
/// Time Complexity: O(nlogn)
14+
/// Space Complexity: O(1)
15+
class Solution {
16+
17+
private:
18+
const long long MOD = 1e9 + 7;
19+
20+
public:
21+
int sumDistance(vector<int>& nums, string s, int d) {
22+
23+
int n = nums.size();
24+
for(int i = 0; i < n; i ++)
25+
if(s[i] == 'R') nums[i] += d; else nums[i] -= d;
26+
sort(nums.begin(), nums.end());
27+
28+
long long right_dis = 0, right_cnt = n - 1;
29+
for(int i = 1; i < n; i ++) right_dis += 0ll + nums[i] - nums[0];
30+
31+
long long res = right_dis % MOD;
32+
for(int i = 1; i < n; i ++){
33+
long long cur = 0ll + nums[i] - nums[i - 1];
34+
35+
right_dis = (right_dis - right_cnt * cur % MOD + MOD) % MOD;
36+
right_cnt --;
37+
38+
res = (res + right_dis) % MOD;
39+
res %= MOD;
40+
}
41+
return res;
42+
}
43+
};
44+
45+
46+
int main() {
47+
48+
return 0;
49+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
project(cpp_2732)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_2732 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/find-a-good-subset-of-the-matrix/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-06-19
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
9+
using namespace std;
10+
11+
12+
/// Ad-Hoc
13+
/// Time Complexity: O(n * 2^b)
14+
/// Space Complexity: O(2^b)
15+
class Solution {
16+
public:
17+
vector<int> goodSubsetofBinaryMatrix(vector<vector<int>>& grid) {
18+
19+
int n = grid.size(), b = grid[0].size();
20+
21+
map<int, int> pattern2index;
22+
for(int i = 0; i < n; i ++){
23+
24+
int value = 0;
25+
for(int j = 0; j < b; j ++)
26+
if(grid[i][j]) value += (1 << j);
27+
28+
if(value == 0) return {{i}};
29+
30+
auto iter = pattern2index.find(value);
31+
if(iter != pattern2index.end()) return {iter->second, i};
32+
33+
for(int s = 0; s < (1 << b); s ++)
34+
if((s & value) == 0) pattern2index[s] = i;
35+
}
36+
return {};
37+
}
38+
};
39+
40+
41+
int main() {
42+
43+
return 0;
44+
}

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
26292629
| 2726 | JavaScript Problem | - | - | - | - |
26302630
| 2727 | JavaScript Problem | - | - | - | - |
26312631
| 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street/description/) | [] | [C++](2001-2500/2728-Count-Houses-in-a-Circular-Street/cpp-2728/) | | |
2632+
| 2729 | [Check if The Number is Fascinating](https://leetcode.com/problems/check-if-the-number-is-fascinating/) | [] | [C++](2001-2500/2729-Check-if-The-Number-is-Fascinating/cpp-2729/) | | |
2633+
| 2730 | [Find the Longest Semi-Repetitive Substring](https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/) | [] | [C++](2001-2500/2730-Find-the-Longest-Semi-Repetitive-Substring/cpp-2730/) | | |
2634+
| 2731 | [Movement of Robots](https://leetcode.com/problems/movement-of-robots/) | [] | [C++](2001-2500/2731-Movement-of-Robots/cpp-2731/) | | |
2635+
| 2732 | [Find a Good Subset of the Matrix](https://leetcode.com/problems/find-a-good-subset-of-the-matrix/) | [] | [C++](2001-2500/2732-Find-a-Good-Subset-of-the-Matrix/cpp-2732/) | | |
26322636
| | | | | | |
26332637
| 2737 | [Find the Closest Marked Node](https://leetcode.com/problems/find-the-closest-marked-node/) | [] | [C++](2001-2500/2737-Find-the-Closest-Marked-Node/cpp-2737/) | | |
26342638
| 2738 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - |

0 commit comments

Comments
 (0)