forked from watashi/AlgoSolution
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |