Skip to content

Commit 09c1ce9

Browse files
authored
Create 2973.Find-Number-of-Coins-to-Place-in-Tree-Nodes.cpp
1 parent c680860 commit 09c1ce9

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using LL = long long;
2+
class Solution {
3+
vector<int>next[20005];
4+
vector<int>children[20005];
5+
vector<LL>rets;
6+
public:
7+
vector<long long> placedCoins(vector<vector<int>>& edges, vector<int>& cost)
8+
{
9+
for (auto& edge: edges)
10+
{
11+
int a = edge[0], b = edge[1];
12+
next[a].push_back(b);
13+
next[b].push_back(a);
14+
}
15+
16+
rets.resize(cost.size());
17+
dfs(0, -1, cost);
18+
return rets;
19+
}
20+
21+
void dfs(int cur, int parent, vector<int>&cost)
22+
{
23+
vector<LL>temp;
24+
for (int nxt: next[cur])
25+
{
26+
if (nxt==parent) continue;
27+
dfs(nxt, cur, cost);
28+
for (int x:children[nxt])
29+
temp.push_back(x);
30+
}
31+
temp.push_back(cost[cur]);
32+
33+
sort(temp.begin(), temp.end());
34+
int n = temp.size();
35+
if (n < 3)
36+
rets[cur] = 1;
37+
else
38+
rets[cur] = max(0LL, max(temp[n-3]*temp[n-2]*temp[n-1], temp[0]*temp[1]*temp[n-1]));
39+
40+
if (n>=1) children[cur].push_back(temp[0]);
41+
if (n>=2) children[cur].push_back(temp[1]);
42+
if (n>=5) children[cur].push_back(temp[n-3]);
43+
if (n>=4) children[cur].push_back(temp[n-2]);
44+
if (n>=3) children[cur].push_back(temp[n-1]);
45+
}
46+
};

0 commit comments

Comments
 (0)