Skip to content

Commit e7a7615

Browse files
committed
2544-2547 solved.
1 parent 1808b87 commit e7a7615

File tree

9 files changed

+170
-0
lines changed

9 files changed

+170
-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.24)
2+
project(cpp_2544)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_2544 main.cpp)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// Source : https://leetcode.com/problems/alternating-digit-sum/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-01-21
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
10+
/// Simulation
11+
/// Time Complexity: O(logn)
12+
/// Space Complexity: O(logn)
13+
class Solution {
14+
public:
15+
int alternateDigitSum(int n) {
16+
17+
string s = to_string(n);
18+
int res = 0;
19+
for(int i = 0; i < s.size(); i ++)
20+
res += (i % 2 ? -1 : 1) * (s[i] - '0');
21+
return res;
22+
}
23+
};
24+
25+
26+
int main() {
27+
28+
return 0;
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
project(cpp_2545)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_2545 main.cpp)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// Source : https://leetcode.com/problems/sort-the-students-by-their-kth-score/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-01-21
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
9+
using namespace std;
10+
11+
12+
/// Sorting
13+
/// Time Complexity: O(mlogm * n)
14+
/// Space Cpomplexity: O(n)
15+
class Solution {
16+
public:
17+
vector<vector<int>> sortTheStudents(vector<vector<int>>& score, int k) {
18+
19+
sort(score.begin(), score.end(),
20+
[k](const vector<int>& s1, const vector<int>& s2){
21+
return s1[k] > s2[k];
22+
});
23+
return score;
24+
}
25+
};
26+
27+
28+
int main() {
29+
30+
return 0;
31+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
project(cpp_2546)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_2546 main.cpp)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/// Source : https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-01-22
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
10+
/// Ad-Hoc
11+
/// Time Complexity: O(n)
12+
/// Space Complexity: O(1)
13+
class Solution {
14+
public:
15+
bool makeStringsEqual(string s, string target) {
16+
17+
int n = s.size() - 1;
18+
if(s == target) return true;
19+
20+
bool s_has1 = false, s_all0 = true;
21+
for(char c: s) if(c == '1') s_has1 = true;
22+
for(char c: s) if(c == '1') s_all0 = false;
23+
24+
bool t_has1 = false, t_all0 = true;
25+
for(char c: target) if(c == '1') t_has1 = true;
26+
for(char c: target) if(c == '1') t_all0 = false;
27+
28+
if(s_has1 && t_all0) return false;
29+
if(t_has1 && s_all0) return false;
30+
return true;
31+
}
32+
};
33+
34+
35+
int main() {
36+
37+
return 0;
38+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
project(cpp_2547)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_2547 main.cpp)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// Source : https://leetcode.com/problems/minimum-cost-to-split-an-array/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-01-22
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <climits>
8+
#include <map>
9+
10+
using namespace std;
11+
12+
13+
/// DP
14+
/// Time Complexity: O(n^2)
15+
/// Space Complexity: O(n)
16+
class Solution {
17+
public:
18+
int minCost(vector<int>& nums, int k) {
19+
20+
int n = nums.size();
21+
22+
vector<int> dp(n + 1, INT_MAX / 2);
23+
dp[n] = 0;
24+
for(int i = n - 1; i >= 0; i --){
25+
map<int, int> f;
26+
int trim_value = 0;
27+
for(int j = i; j >= 0; j --){
28+
f[nums[j]] ++;
29+
int len = f[nums[j]];
30+
if(len == 2) trim_value += 2;
31+
else if(len > 2) trim_value ++;
32+
dp[j] = min(dp[j], trim_value + k + dp[i + 1]);
33+
}
34+
}
35+
return dp[0];
36+
}
37+
};
38+
39+
40+
int main() {
41+
42+
return 0;
43+
}

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
24252425
| 2541 | [Minimum Operations to Make Array Equal II](https://leetcode.com/problems/minimum-operations-to-make-array-equal-ii/) | [] | [C++](2001-2500/2541-Minimum-Operations-to-Make-Array-Equal-II/cpp-2541/) | | |
24262426
| 2542 | [Maximum Subsequence Score](https://leetcode.com/problems/maximum-subsequence-score/) | [] | [C++](2001-2500/2542-Maximum-Subsequence-Score/cpp-2542/) | | |
24272427
| | | | | | |
2428+
| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum/) | [] | [C++](2001-2500/2544-Alternating-Digit-Sum/cpp-2544/) | | |
2429+
| 2545 | [Sort the Students by Their Kth Score](https://leetcode.com/problems/sort-the-students-by-their-kth-score/) | [] | [C++](2001-2500/2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545/) | | |
2430+
| 2546 | [Apply Bitwise Operations to Make Strings Equal](https://leetcode.com/problems/apply-bitwise-operations-to-make-strings-equal/) | [] | [C++](2001-2500/2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546/) | | |
2431+
| 2547 | [Minimum Cost to Split an Array](https://leetcode.com/problems/minimum-cost-to-split-an-array/) | [] | [C++](2001-2500/2547-Minimum-Cost-to-Split-an-Array/cpp-2547/) | | |
2432+
| | | | | | |
24282433

24292434
### 力扣中文站比赛 [传送门](LC/)
24302435

0 commit comments

Comments
 (0)