File tree Expand file tree Collapse file tree 10 files changed +218
-0
lines changed
2349-Design-a-Number-Container-System/cpp-2349
2351-First-Letter-to-Appear-Twice/cpp-2351
2352-Equal-Row-and-Column-Pairs/cpp-2352
2353-Design-a-Food-Rating-System/cpp-2353
2354-Number-of-Excellent-Pairs/cpp-2354 Expand file tree Collapse file tree 10 files changed +218
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ using namespace std;
10
10
11
11
12
12
// / Using Map
13
+ // / Time Complecity: O(logn) for every operation
14
+ // / Space Compelxity: O(n)
13
15
class NumberContainers {
14
16
15
17
private:
Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.22 )
2
+ project (A )
3
+
4
+ set (CMAKE_CXX_STANDARD 14 )
5
+
6
+ add_executable (A main.cpp )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.22 )
2
+ project (B )
3
+
4
+ set (CMAKE_CXX_STANDARD 14 )
5
+
6
+ add_executable (B main.cpp )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.22 )
2
+ project (C )
3
+
4
+ set (CMAKE_CXX_STANDARD 14 )
5
+
6
+ add_executable (C main.cpp )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.22 )
2
+ project (D )
3
+
4
+ set (CMAKE_CXX_STANDARD 14 )
5
+
6
+ add_executable (D main.cpp )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -2203,6 +2203,10 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
2203
2203
| 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/ ) | | |
2204
2204
| 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/ ) | | |
2205
2205
| 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/ ) | | |
2206
2210
| | | | | | |
2207
2211
2208
2212
## 力扣中文站比赛
You can’t perform that action at this time.
0 commit comments