Skip to content

Commit

Permalink
fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed Aug 4, 2024
1 parent 562c93d commit 0a15a24
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions leetcode/biweekly/120/d/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

这两种情况再和 $0$ 取最大值,即为返回值。

由上诉讨论可知,只需要知道 $5$ 个数,就能算出三数之积的最大值。每棵子树只需要返回它最小的 $2$ 个 $\textit{cost}$ 值和最大的 $3$ 个 $\textit{cost}$ 值就行,其余数字可以舍弃。
由上述讨论可知,只需要知道 $5$ 个数,就能算出三数之积的最大值。每棵子树只需要返回它最小的 $2$ 个 $\textit{cost}$ 值和最大的 $3$ 个 $\textit{cost}$ 值就行,其余数字可以舍弃。

对于一棵子树,把它的所有儿子子树的返回值与当前节点的 $\textit{cost}$ 值排序后,按照三数之积的方法,就得到了当前节点的答案。

Expand Down Expand Up @@ -83,35 +83,35 @@ class Solution {
```cpp [sol-C++]
class Solution {
public:
vector<long long> placedCoins(vector<vector<int>> &edges, vector<int> &cost) {
vector<long long> placedCoins(vector<vector<int>>& edges, vector<int>& cost) {
int n = cost.size();
vector<vector<int>> g(n);
for (auto &e: edges) {
for (auto& e: edges) {
int x = e[0], y = e[1];
g[x].push_back(y);
g[y].push_back(x);
}

vector<long long> ans(n, 1);
function<vector<int>(int, int)> dfs = [&](int x, int fa) -> vector<int> {
auto dfs = [&](auto&& dfs, int x, int fa) -> vector<int> {
vector<int> a = {cost[x]};
for (int y: g[x]) {
if (y != fa) {
auto res = dfs(y, x);
auto res = dfs(dfs, y, x);
a.insert(a.end(), res.begin(), res.end());
}
}
sort(a.begin(), a.end());
ranges::sort(a);
int m = a.size();
if (m >= 3) {
ans[x] = max({(long long) a[m - 3] * a[m - 2] * a[m - 1], (long long) a[0] * a[1] * a[m - 1], 0LL});
ans[x] = max(max((long long) a[m - 3] * a[m - 2] * a[m - 1], (long long) a[0] * a[1] * a[m - 1]), 0LL);
}
if (m > 5) {
a = {a[0], a[1], a[m - 3], a[m - 2], a[m - 1]};
}
return a;
};
dfs(0, -1);
dfs(dfs, 0, -1);
return ans;
}
};
Expand Down Expand Up @@ -158,3 +158,22 @@ func placedCoins(edges [][]int, cost []int) []int64 {

- 时间复杂度:$\mathcal{O}(n\log n)$,其中 $n$ 为 $\textit{nums}$ 的长度。瓶颈在排序上,如果手动维护最小的两个数和最大的三个数可以做到 $\mathcal{O}(n)$。
- 空间复杂度:$\mathcal{O}(n)$。

## 分类题单

[如何科学刷题?](https://leetcode.cn/circle/discuss/RvFUtj/)

1. [滑动窗口(定长/不定长/多指针)](https://leetcode.cn/circle/discuss/0viNMK/)
2. [二分算法(二分答案/最小化最大值/最大化最小值/第K小)](https://leetcode.cn/circle/discuss/SqopEo/)
3. [单调栈(基础/矩形面积/贡献法/最小字典序)](https://leetcode.cn/circle/discuss/9oZFK9/)
4. [网格图(DFS/BFS/综合应用)](https://leetcode.cn/circle/discuss/YiXPXW/)
5. [位运算(基础/性质/拆位/试填/恒等式/思维)](https://leetcode.cn/circle/discuss/dHn9Vk/)
6. [图论算法(DFS/BFS/拓扑排序/最短路/最小生成树/二分图/基环树/欧拉路径)](https://leetcode.cn/circle/discuss/01LUak/)
7. [动态规划(入门/背包/状态机/划分/区间/状压/数位/数据结构优化/树形/博弈/概率期望)](https://leetcode.cn/circle/discuss/tXLS3i/)
8. [常用数据结构(前缀和/差分/栈/队列/堆/字典树/并查集/树状数组/线段树)](https://leetcode.cn/circle/discuss/mOr1u6/)
9. [数学算法(数论/组合/概率期望/博弈/计算几何/随机算法)](https://leetcode.cn/circle/discuss/IYT3ss/)
10. [贪心算法(基本贪心策略/反悔/区间/字典序/数学/思维/脑筋急转弯/构造)](https://leetcode.cn/circle/discuss/g6KTKL/)

[我的题解精选(已分类)](https://github.com/EndlessCheng/codeforces-go/blob/master/leetcode/SOLUTIONS.md)

欢迎关注 [B站@灵茶山艾府](https://space.bilibili.com/206214)

0 comments on commit 0a15a24

Please sign in to comment.