Skip to content

Commit

Permalink
LeetCode Weekly Contest 297
Browse files Browse the repository at this point in the history
  • Loading branch information
watashi committed Dec 24, 2023
1 parent 0979c1d commit 64dba4d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
14 changes: 14 additions & 0 deletions leetcode/weekly-297/2303.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
double calculateTax(vector<vector<int>>& brackets, int income) {
int64_t tax = 0;
int pre = 0;
for (const auto& i : brackets) {
if (income > pre) {
tax += (min(i[0], income) - pre)* (int64_t)i[1];
}
pre = i[0];
}
return tax / 100.0;
}
};
20 changes: 20 additions & 0 deletions leetcode/weekly-297/2304.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int minPathCost(vector<vector<int>>& grid, vector<vector<int>>& moveCost) {
int m = grid.size(), n = grid[0].size();
vector<int> cost = grid[0];
for (int i = 0; i < m - 1; ++i) {
vector<int> next(n, numeric_limits<int>::max());
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
next[k] = min(next[k], cost[j] + moveCost[grid[i][j]][k]);
}
}
cost = std::move(next);
for (int j = 0; j < n; ++j) {
cost[j] += grid[i + 1][j];
}
}
return *min_element(cost.begin(), cost.end());
}
};
27 changes: 27 additions & 0 deletions leetcode/weekly-297/2305.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int distributeCookies(vector<int>& cookies, int k) {
int n = cookies.size();
int m = 1 << n;
vector<int> s(m, 0);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if ((i >> j) & 1) {
s[i] += cookies[j];
}
}
}
vector<vector<int>> dp(k + 1, vector<int>(m, numeric_limits<int>::max() / 2));
dp[0][0] = 0;
for (int i = 1; i <= k; ++i) {
dp[i] = dp[i - 1];
for (int j = 0; j < m; ++j) {
for (int k = j; k > 0; k = (k - 1) & j) {
dp[i][j] = min(dp[i][j], max(dp[i - 1][j ^ k], s[k]));
}
// printf("[%d][%d] = %d\n", i, j, dp[i][j]);
}
}
return dp[k][m - 1];
}
};
40 changes: 40 additions & 0 deletions leetcode/weekly-297/2306.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public:
long long distinctNames(vector<string>& ideas) {
static constexpr int CHARSET = 26;
unordered_map<string, int> mp;
for (const auto& i : ideas) {
mp[i.substr(1)] |= 1 << (i[0] - 'a');
}

vector<vector<long long>> c(CHARSET, vector<long long>(CHARSET, 0));
vector<int> x, y;
x.reserve(CHARSET);
y.reserve(CHARSET);
for (const auto& i : mp) {
x.clear();
y.clear();
for (int j = 0; j < CHARSET; ++j) {
if ((i.second >> j) & 1) {
x.push_back(j);
}
else {
y.push_back(j);
}
}
for (int xi : x) {
for (int yi : y) {
++c[xi][yi];
}
}
}

long long ans = 0;
for (int i = 0; i < CHARSET; ++i) {
for (int j = 0; j < CHARSET; ++j) {
ans += c[i][j] * c[j][i];
}
}
return ans;
}
};

0 comments on commit 64dba4d

Please sign in to comment.