Skip to content

Commit 74cf562

Browse files
committed
2303-2306 solved.
1 parent 6582bc3 commit 74cf562

File tree

9 files changed

+215
-1
lines changed

9 files changed

+215
-1
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/// Source : https://leetcode.com/problems/calculate-amount-paid-in-taxes/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-11
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+
double calculateTax(vector<vector<int>>& brackets, int income) {
17+
18+
if(income <= brackets[0][0]){
19+
return income * brackets[0][1] / (double)100.0;
20+
}
21+
22+
int left = income - brackets[0][0];
23+
double res = brackets[0][0] * brackets[0][1] / (double)100.0;
24+
for(int i = 1; i < brackets.size() && left; i ++){
25+
int x = brackets[i][0] - brackets[i - 1][0];
26+
int t = min(x, left);
27+
left -= t;
28+
res += t * brackets[i][1] / (double)100.0;
29+
}
30+
return res;
31+
}
32+
};
33+
34+
35+
int main() {
36+
37+
vector<vector<int>> br1 = {{3, 50}, {7, 10}, {12, 25}};
38+
cout << Solution().calculateTax(br1, 10) << '\n';
39+
40+
return 0;
41+
}
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/minimum-path-cost-in-a-grid/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-11
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <climits>
8+
9+
using namespace std;
10+
11+
12+
/// DP
13+
/// Time Complexity: O(n * m * m)
14+
/// Space Compelxity: O(n * m)
15+
class Solution {
16+
public:
17+
int minPathCost(vector<vector<int>>& grid, vector<vector<int>>& moveCost) {
18+
19+
int R = grid.size(), C = grid[0].size();
20+
vector<vector<int>> dp(R, vector<int>(C, INT_MAX));
21+
for(int j = 0; j < C; j ++) dp[0][j] = grid[0][j];
22+
23+
for(int i = 0; i + 1 < R; i ++)
24+
for(int j = 0; j < C; j ++){
25+
for(int k = 0; k < C; k ++)
26+
dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + moveCost[grid[i][j]][k] + grid[i + 1][k]);
27+
}
28+
return *min_element(dp[R - 1].begin(), dp[R - 1].end());
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// Source : https://leetcode.com/problems/fair-distribution-of-cookies/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-11
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Memoization
12+
/// Time Complexity: O(k * 3^n)
13+
/// Space Complexity: O(k * 2^n)
14+
class Solution {
15+
public:
16+
int distributeCookies(vector<int>& cookies, int k) {
17+
18+
int n = cookies.size();
19+
20+
vector<int> table(1 << n, 0);
21+
for(int state = 1; state < (1 << n); state ++){
22+
int p = __builtin_ffs(state) - 1;
23+
table[state] = table[state - (1 << p)] + cookies[p];
24+
}
25+
26+
vector<vector<int>> dp(k + 1, vector<int>(1 << n, -1));
27+
return dfs((1 << n) - 1, k, table, dp);
28+
}
29+
30+
private:
31+
int dfs(int state, int k, const vector<int>& table, vector<vector<int>>& dp){
32+
33+
if(k == 1) return table[state];
34+
if(dp[k][state] != -1) return dp[k][state];
35+
36+
int res = table[state];
37+
for (int s = state; s; s = (s - 1) & state) {
38+
res = min(res, max(table[s], dfs(state - s, k - 1, table, dp)));
39+
}
40+
return dp[k][state] = res;
41+
}
42+
};
43+
44+
45+
int main() {
46+
47+
return 0;
48+
}
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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/// Source : https://leetcode.com/problems/naming-a-company/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-11
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <unordered_set>
8+
#include <numeric>
9+
10+
using namespace std;
11+
12+
13+
/// DP
14+
/// Time Complexity: O(n)
15+
/// Space Complexity: O(n)
16+
class Solution {
17+
public:
18+
long long distinctNames(vector<string>& ideas) {
19+
20+
int n = ideas.size();
21+
22+
unordered_set<string> ideas_set(ideas.begin(), ideas.end());
23+
24+
vector<vector<bool>> can_change(26, vector<bool>(n, false));
25+
vector<vector<int>> ok(26, vector<int>(26, 0));
26+
for(int i = 0; i < n; i ++){
27+
char o_first = ideas[i][0];
28+
for(char c = 'a'; c <= 'z'; c ++){
29+
ideas[i][0] = c;
30+
if(c != o_first && !ideas_set.count(ideas[i])){
31+
can_change[c - 'a'][i] = true;
32+
ok[o_first - 'a'][c - 'a'] ++;
33+
}
34+
35+
}
36+
ideas[i][0] = o_first;
37+
}
38+
39+
long long res = 0;
40+
for(int i = 0; i < n; i ++){
41+
char o_first = ideas[i][0];
42+
for(char c = 'a'; c <= 'z'; c ++)
43+
if(can_change[c - 'a'][i]) res += ok[c - 'a'][o_first - 'a'];
44+
}
45+
return res;
46+
}
47+
};
48+
49+
50+
int main() {
51+
52+
vector<string> ideas1 = {"coffee","donuts","time","toffee"};
53+
cout << Solution().distinctNames(ideas1) << '\n';
54+
// 6
55+
56+
vector<string> ideas2 = {"lack","back"};
57+
cout << Solution().distinctNames(ideas2) << '\n';
58+
// 0
59+
60+
return 0;
61+
}

readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2141,11 +2141,15 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
21412141
| 2295 | [Replace Elements in an Array](https://leetcode.com/problems/replace-elements-in-an-array/) | [] | [C++](2001-2500/2295-Replace-Elements-in-an-Array/cpp-2295/) | | |
21422142
| 2296 | [Design a Text Editor](https://leetcode.com/problems/design-a-text-editor/) | [] | [C++](2001-2500/2296-Design-a-Text-Editor/cpp-2296/) | | |
21432143
| 2297 | [Jump Game IX](https://leetcode.com/problems/jump-game-ix/) | [] | [C++](2001-2500/2297-Jump-Game-IX/cpp-2297/) | | |
2144-
| | | | | | |
2144+
| 2298 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - |
21452145
| 2299 | [Strong Password Checker II](https://leetcode.com/problems/strong-password-checker-ii/) | [] | [C++](2001-2500/2299-Strong-Password-Checker-II/cpp-2299/) | | |
21462146
| 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions/) | [] | [C++](2001-2500/2300-Successful-Pairs-of-Spells-and-Potions/cpp-2300/) | | |
21472147
| 2301 | [Match Substring After Replacement](https://leetcode.com/problems/match-substring-after-replacement/) | [] | [C++](2001-2500/2301-Match-Substring-After-Replacement/cpp-2301/) | | |
21482148
| 2302 | [Count Subarrays With Score Less Than K](https://leetcode.com/problems/count-subarrays-with-score-less-than-k/) | [] | [C++](2001-2500/2302-Count-Subarrays-With-Score-Less-Than-K/cpp-2302/) | | |
2149+
| 2303 | [Calculate Amount Paid in Taxes](https://leetcode.com/problems/calculate-amount-paid-in-taxes/) | [] | [C++](2001-2500/2303-Calculate-Amount-Paid-in-Taxes/cpp-2303/) | | |
2150+
| 2304 | [Minimum Path Cost in a Grid](https://leetcode.com/problems/minimum-path-cost-in-a-grid/) | [] | [C++](2001-2500/2304-Minimum-Path-Cost-in-a-Grid/cpp-2304/) | | |
2151+
| 2305 | [Fair Distribution of Cookies](https://leetcode.com/problems/fair-distribution-of-cookies/) | [] | [C++](2001-2500/2305-Fair-Distribution-of-Cookies/cpp-2305/) | | |
2152+
| 2306 | [Naming a Company](https://leetcode.com/problems/naming-a-company/) | [] | [C++](2001-2500/2306-Naming-a-Company/cpp-2306/) | | |
21492153
| | | | | | |
21502154

21512155
## 力扣中文站比赛

0 commit comments

Comments
 (0)