Skip to content

Commit 911f8df

Browse files
authored
Create 2973-find-number-of-coins-to-place-in-tree-nodes.js
1 parent a60ab31 commit 911f8df

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* @param {number[][]} edges
3+
* @param {number[]} cost
4+
* @return {number[]}
5+
*/
6+
var placedCoins = function (edges, cost) {
7+
let tree = buildRootedTree(buildGraph(cost.length, edges), 0)
8+
let res = Array(cost.length).fill()
9+
10+
function solve(root) {
11+
let node = tree[root]
12+
let c = cost[root]
13+
14+
if (c > 0) node.max1 = c
15+
else node.min1 = c
16+
17+
for (let child of node.aNodes) {
18+
solve(child)
19+
let childNode = tree[child]
20+
opt(node, childNode.max1)
21+
opt(node, childNode.max2)
22+
opt(node, childNode.max3)
23+
opt(node, childNode.min1)
24+
opt(node, childNode.min2)
25+
opt(node, childNode.min3)
26+
}
27+
28+
let cnt =
29+
!!node.min1 +
30+
!!node.min2 +
31+
!!node.min3 +
32+
!!node.max1 +
33+
!!node.max2 +
34+
!!node.max3
35+
if (cnt < 3) {
36+
res[root] = 1
37+
return
38+
}
39+
40+
res[root] = 0
41+
let v = node.max1 * node.max2 * node.max3
42+
if (v > res[root]) res[root] = v
43+
v = node.max1 * node.min1 * node.min2
44+
if (v > res[root]) res[root] = v
45+
}
46+
47+
solve(0)
48+
return res
49+
}
50+
51+
/**
52+
* @typedef {{ aNodes: number[] }} TGraphNode
53+
* @param {number} n
54+
* @param {[number,number,number?][]} edges
55+
* @return {TGraphNode[]}
56+
*/
57+
function buildGraph(n, edges) {
58+
/** @type {TGraphNode[]} */
59+
let nodes = []
60+
for (let i = 0; i < n; i++) nodes.push({ aNodes: [] })
61+
62+
let m = edges.length
63+
for (let i = 0; i < m; i++) {
64+
let [u, v] = edges[i]
65+
nodes[u].aNodes.push(v)
66+
nodes[v].aNodes.push(u)
67+
}
68+
69+
return nodes
70+
}
71+
72+
/**
73+
* @typedef {{ parent: number, min1, min2, min3, max1, max2, max3 }} TTreeNode
74+
* @param {(TGraphNode & TTreeNode)[]} graph
75+
* @param {number} root
76+
* @param {number?} parent
77+
* @return {(TGraphNode & TTreeNode)[]}
78+
*/
79+
function buildRootedTree(graph, root, parent) {
80+
let node = graph[root]
81+
node.parent = parent
82+
83+
let m = node.aNodes.length
84+
let parentIndex = undefined
85+
for (let i = 0; i < m; i++) {
86+
if (node.aNodes[i] == parent) parentIndex = i
87+
else buildRootedTree(graph, node.aNodes[i], root)
88+
}
89+
90+
if (parentIndex != undefined) {
91+
node.aNodes[parentIndex] = node.aNodes[m - 1]
92+
node.aNodes.pop()
93+
}
94+
95+
node.max1 = 0
96+
node.max2 = 0
97+
node.max3 = 0
98+
node.min1 = 0
99+
node.min2 = 0
100+
node.min3 = 0
101+
return graph
102+
}
103+
104+
/**
105+
* @param {TTreeNode} node
106+
* @param {number} cost
107+
*/
108+
function opt(node, cost) {
109+
if (!cost) return
110+
if (cost > 0) {
111+
if (cost >= node.max1) {
112+
node.max3 = node.max2
113+
node.max2 = node.max1
114+
node.max1 = cost
115+
} else if (cost >= node.max2) {
116+
node.max3 = node.max2
117+
node.max2 = cost
118+
} else if (cost > node.max3) {
119+
node.max3 = cost
120+
}
121+
} else {
122+
if (cost <= node.min1) {
123+
node.min3 = node.min2
124+
node.min2 = node.min1
125+
node.min1 = cost
126+
} else if (cost <= node.min2) {
127+
node.min3 = node.min2
128+
node.min2 = cost
129+
} else if (cost < node.min3) {
130+
node.min3 = cost
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)