Skip to content

Commit f04ecb3

Browse files
committed
2351-2354 added.
1 parent 2a8d1e4 commit f04ecb3

File tree

10 files changed

+218
-0
lines changed

10 files changed

+218
-0
lines changed

2001-2500/2349-Design-a-Number-Container-System/cpp-2349/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ using namespace std;
1010

1111

1212
/// Using Map
13+
/// Time Complecity: O(logn) for every operation
14+
/// Space Compelxity: O(n)
1315
class NumberContainers {
1416

1517
private:
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// Source : https://leetcode.com/problems/first-letter-to-appear-twice/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-23
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Simulation
12+
/// Time Complexity: O(n)
13+
/// Space Complexity: O(1)
14+
class Solution {
15+
public:
16+
char repeatedCharacter(string s) {
17+
18+
vector<int> f(26, 0);
19+
for(char c: s){
20+
f[c - 'a'] ++;
21+
if(f[c - 'a'] == 2) return c;
22+
}
23+
return 'a';
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.22)
2+
project(B)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(B main.cpp)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// Source : https://leetcode.com/problems/equal-row-and-column-pairs/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-23
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Brute Force
12+
/// Time Complexity: O(n^3)
13+
/// Space Complexity: O(n^2)
14+
class Solution {
15+
public:
16+
int equalPairs(vector<vector<int>>& grid) {
17+
18+
int n = grid.size();
19+
20+
vector<vector<int>> cols(n, vector<int>(n));
21+
for(int j = 0; j < n; j ++)
22+
for(int i = 0; i < n; i ++) cols[j][i] = grid[i][j];
23+
24+
int res = 0;
25+
for(int i = 0; i < n; i ++)
26+
for(int j = 0; j < n; j ++)
27+
res += grid[i] == cols[j];
28+
return res;
29+
}
30+
};
31+
32+
33+
int main() {
34+
35+
return 0;
36+
}
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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/// Source : https://leetcode.com/problems/design-a-food-rating-system/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-23
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <map>
8+
#include <set>
9+
10+
using namespace std;
11+
12+
13+
/// Using Map
14+
/// Time Complexity: O(logn) for every operation
15+
/// Space Complexity: O(n)
16+
class FoodRatings {
17+
18+
private:
19+
map<string, map<int, set<string>>> tree;
20+
map<string, pair<string, int>> food;
21+
22+
public:
23+
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
24+
25+
int n = foods.size();
26+
for(int i = 0; i < n; i ++){
27+
food[foods[i]] = {cuisines[i], ratings[i]};
28+
tree[cuisines[i]][ratings[i]].insert(foods[i]);
29+
}
30+
}
31+
32+
void changeRating(string food_name, int newRating) {
33+
34+
pair<string, int> p = food[food_name];
35+
string cuision = p.first;
36+
int oldRating = p.second;
37+
38+
tree[cuision][oldRating].erase(food_name);
39+
if(tree[cuision][oldRating].empty()) tree[cuision].erase(oldRating);
40+
tree[cuision][newRating].insert(food_name);
41+
42+
food[food_name].second = newRating;
43+
}
44+
45+
string highestRated(string cuisine) {
46+
return *(tree[cuisine].rbegin()->second.begin());
47+
}
48+
};
49+
50+
51+
int main() {
52+
53+
return 0;
54+
}
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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/// Source : https://leetcode.com/problems/number-of-excellent-pairs/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-24
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <set>
8+
#include <map>
9+
10+
using namespace std;
11+
12+
13+
/// bitwise
14+
/// Time Compelxity: O(nlogn(max_a))
15+
/// Space Complexity: O(n)
16+
class Solution {
17+
public:
18+
long long countExcellentPairs(vector<int>& nums, int k) {
19+
20+
map<int, int> ones;
21+
for(int e: nums){
22+
if(ones.count(e)) continue;
23+
ones[e] = get_ones(e);
24+
}
25+
26+
vector<int> cnt(63, 0);
27+
for(const pair<int, int>& p: ones)
28+
cnt[p.second] ++;
29+
30+
for(int i = 61; i >= 0; i --)
31+
cnt[i] += cnt[i + 1];
32+
33+
long long res = 0;
34+
for(const pair<int, int>& p: ones){
35+
int e = p.first;
36+
int one = p.second;
37+
38+
int t = max(k - one, 0);
39+
res += cnt[t];
40+
}
41+
return res;
42+
}
43+
44+
private:
45+
int get_ones(int e){
46+
int res = 0;
47+
while(e){
48+
res ++;
49+
e &= (e - 1);
50+
}
51+
return res;
52+
}
53+
};
54+
55+
56+
int main() {
57+
58+
vector<int> nums1 = {1, 2, 3, 1};
59+
cout << Solution().countExcellentPairs(nums1, 3) << '\n';
60+
// 5
61+
62+
vector<int> nums2 = {5, 1, 1};
63+
cout << Solution().countExcellentPairs(nums2, 10) << '\n';
64+
// 0
65+
66+
return 0;
67+
}

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
22032203
| 2348 | [Number of Zero-Filled Subarrays](https://leetcode.com/problems/number-of-zero-filled-subarrays/) | [] | [C++](2001-2500/2348-Number-of-Zero-Filled-Subarrays/cpp-2348/) | | |
22042204
| 2349 | [Design a Number Container System](https://leetcode.com/problems/design-a-number-container-system/) | [] | [C++](2001-2500/2349-Design-a-Number-Container-System/cpp-2349/) | | |
22052205
| 2350 | [Shortest Impossible Sequence of Rolls](https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/) | [] | [C++](2001-2500/2350-Shortest-Impossible-Sequence-of-Rolls/cpp-2350/) | | |
2206+
| 2351 | [First Letter to Appear Twice](https://leetcode.com/problems/first-letter-to-appear-twice/) | [] | [C++](2001-2500/2351-First-Letter-to-Appear-Twice/cpp-2351/) | | |
2207+
| 2352 | [Equal Row and Column Pairs](https://leetcode.com/problems/equal-row-and-column-pairs/) | [] | [C++](2001-2500/2352-Equal-Row-and-Column-Pairs/cpp-2352/) | | |
2208+
| 2353 | [Design a Food Rating System](https://leetcode.com/problems/design-a-food-rating-system/) | [] | [C++](2001-2500/2353-Design-a-Food-Rating-System/cpp-2353/) | | |
2209+
| 2354 | [Number of Excellent Pairs](https://leetcode.com/problems/number-of-excellent-pairs/) | [] | [C++](2001-2500/2354-Number-of-Excellent-Pairs/cpp-2354/) | | |
22062210
| | | | | | |
22072211

22082212
## 力扣中文站比赛

0 commit comments

Comments
 (0)