Skip to content

Commit f8165ca

Browse files
committed
2341-2344 added.
1 parent 17d7c65 commit f8165ca

File tree

10 files changed

+255
-0
lines changed

10 files changed

+255
-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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// Source : https://leetcode.com/problems/maximum-number-of-pairs-in-array/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-16
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
9+
using namespace std;
10+
11+
12+
/// Using Map
13+
/// Time Complexity: O(n)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
vector<int> numberOfPairs(vector<int>& nums) {
18+
19+
map<int, int> f;
20+
for(int e: nums) f[e] ++;
21+
22+
vector<int> res(2, 0);
23+
for(const pair<int, int>& p: f){
24+
res[0] += p.second / 2;
25+
res[1] += p.second % 2;
26+
}
27+
return res;
28+
}
29+
};
30+
31+
32+
int main() {
33+
34+
return 0;
35+
}
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 main2.cpp)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/// Source : https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-16
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
#include <algorithm>
9+
10+
using namespace std;
11+
12+
13+
/// Using Map
14+
/// Time Complexity: O(nlogn)
15+
/// Space Complexity: O(n)
16+
class Solution {
17+
public:
18+
int maximumSum(vector<int>& nums) {
19+
20+
map<int, vector<int>> f;
21+
for(int e: nums) f[dsum(e)].push_back(e);
22+
23+
for(const pair<int, vector<int>>& p: f){
24+
int sum = p.first;
25+
sort(f[sum].begin(), f[sum].end(), greater<int>());
26+
}
27+
28+
int res = -1;
29+
for(const pair<int, vector<int>>& p: f){
30+
if(p.second.size() >= 2){
31+
res = max(res, p.second[0] + p.second[1]);
32+
}
33+
}
34+
return res;
35+
}
36+
37+
private:
38+
int dsum(int x){
39+
int res = 0;
40+
while(x){
41+
res += x % 10;
42+
x /= 10;
43+
}
44+
return res;
45+
}
46+
};
47+
48+
49+
int main() {
50+
51+
return 0;
52+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/// Source : https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-16
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
#include <algorithm>
9+
10+
using namespace std;
11+
12+
13+
/// Using Map, no sorting
14+
/// Time Complexity: O(nlogn)
15+
/// Space Complexity: O(n)
16+
class Solution {
17+
public:
18+
int maximumSum(vector<int>& nums) {
19+
20+
map<int, pair<int, int>> f;
21+
for(int e: nums){
22+
int sum = dsum(e);
23+
auto iter = f.find(sum);
24+
if(iter == f.end()) f[sum] = {e, -1};
25+
else if(e > iter->second.first){
26+
iter->second.second = iter->second.first;
27+
iter->second.first = e;
28+
}
29+
else{
30+
iter->second.second = max(e, iter->second.second);
31+
}
32+
}
33+
34+
int res = -1;
35+
for(const pair<int, pair<int, int>>& p: f){
36+
if(p.second.second != -1){
37+
res = max(res, p.second.first + p.second.second);
38+
}
39+
}
40+
return res;
41+
}
42+
43+
private:
44+
int dsum(int x){
45+
int res = 0;
46+
while(x){
47+
res += x % 10;
48+
x /= 10;
49+
}
50+
return res;
51+
}
52+
};
53+
54+
55+
int main() {
56+
57+
return 0;
58+
}
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// Source : https://leetcode.com/problems/query-kth-smallest-trimmed-number/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-16
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Brute Force
12+
/// Time Complexity: O(q * nlogn * len)
13+
/// Space Complexity: O(n)
14+
class Solution {
15+
public:
16+
vector<int> smallestTrimmedNumbers(vector<string>& nums, vector<vector<int>>& queries) {
17+
18+
int n = nums.size(), len = nums[0].size();
19+
20+
vector<int> res(queries.size());
21+
for(int i = 0; i < queries.size(); i ++){
22+
res[i] = solve(n, len, nums, queries[i][0] - 1, queries[i][1]);
23+
}
24+
return res;
25+
}
26+
27+
private:
28+
int solve(int n, int len, const vector<string>& nums, int k, int x){
29+
30+
vector<pair<string, int>> data(n);
31+
for(int i = 0; i < n; i ++)
32+
data[i] = {nums[i].substr(len - x), i};
33+
sort(data.begin(), data.end());
34+
return data[k].second;
35+
}
36+
};
37+
38+
39+
int main() {
40+
41+
return 0;
42+
}
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/// Source : https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-16
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
9+
using namespace std;
10+
11+
12+
/// Sorting and Greedy
13+
/// Time Complexity: O(nlog(max(numsDivide)) + nlogn)
14+
/// Space Complexity: O(log(max(numsDivide)))
15+
class Solution {
16+
public:
17+
int minOperations(vector<int>& nums, vector<int>& numsDivide) {
18+
19+
int t = numsDivide[0];
20+
for(int i = 1; i < numsDivide.size(); i ++)
21+
t = gcd(min(t, numsDivide[i]), max(t, numsDivide[i]));
22+
23+
sort(nums.begin(), nums.end());
24+
for(int i = 0; i < nums.size(); i ++)
25+
if(t % nums[i] == 0) return i;
26+
return -1;
27+
}
28+
29+
private:
30+
int gcd(int a, int b){
31+
if (a == 0) return b;
32+
return gcd(b % a, a);
33+
}
34+
};
35+
36+
37+
int main() {
38+
39+
return 0;
40+
}

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
21892189
| 2338 | [Count the Number of Ideal Arrays](https://leetcode.com/problems/count-the-number-of-ideal-arrays/) | [] | [C++](2001-2500/2338-Count-the-Number-of-Ideal-Arrays/cpp-2338/) | | |
21902190
| 2339 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - |
21912191
| 2340 | [Minimum Adjacent Swaps to Make a Valid Array](https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/) | [] | [C++](2001-2500/2340-Minimum-Adjacent-Swaps-to-Make-a-Valid-Array/cpp-2340/) | | |
2192+
| 2341 | [Maximum Number of Pairs in Array](https://leetcode.com/problems/maximum-number-of-pairs-in-array/) | [] | [C++](2001-2500/2341-Maximum-Number-of-Pairs-in-Array/cpp-2341/) | | |
2193+
| 2342 | [Max Sum of a Pair With Equal Sum of Digits](https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/) | [] | [C++](2001-2500/2342-Max-Sum-of-a-Pair-With-Equal-Sum-of-Digits/cpp-2342/) | | |
2194+
| 2343 | [Query Kth Smallest Trimmed Number](https://leetcode.com/problems/query-kth-smallest-trimmed-number/) | [] | [C++](2001-2500/2343-Query-Kth-Smallest-Trimmed-Number/cpp-2343/) | | |
2195+
| 2344 | [Minimum Deletions to Make Array Divisible](https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/) | [] | [C++](2001-2500/2344-Minimum-Deletions-to-Make-Array-Divisible/cpp-2344/) | | |
21922196
| | | | | | |
21932197

21942198
## 力扣中文站比赛

0 commit comments

Comments
 (0)