File tree Expand file tree Collapse file tree 9 files changed +170
-0
lines changed
2544-Alternating-Digit-Sum/cpp-2544
2545-Sort-the-Students-by-Their-Kth-Score/cpp-2545
2546-Apply-Bitwise-Operations-to-Make-Strings-Equal/cpp-2546
2547-Minimum-Cost-to-Split-an-Array/cpp-2547 Expand file tree Collapse file tree 9 files changed +170
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -2425,6 +2425,11 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
2425
2425
| 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/ ) | | |
2426
2426
| 2542 | [ Maximum Subsequence Score] ( https://leetcode.com/problems/maximum-subsequence-score/ ) | [ 无] | [ C++] ( 2001-2500/2542-Maximum-Subsequence-Score/cpp-2542/ ) | | |
2427
2427
| | | | | | |
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
+ | | | | | | |
2428
2433
2429
2434
### 力扣中文站比赛 [ 传送门] ( LC/ )
2430
2435
You can’t perform that action at this time.
0 commit comments