Skip to content

Commit 5cbae3a

Browse files
authored
Update 2065-maximum-path-quality-of-a-graph.js
1 parent d7348ee commit 5cbae3a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

2065-maximum-path-quality-of-a-graph.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
/**
2+
* @param {number[]} values
3+
* @param {number[][]} edges
4+
* @param {number} maxTime
5+
* @return {number}
6+
*/
7+
const maximalPathQuality = function(values, edges, maxTime) {
8+
const graph = {}, n = values.length
9+
for(const [u, v, t] of edges) {
10+
if(graph[u] == null) graph[u] = []
11+
if(graph[v] == null) graph[v] = []
12+
graph[u].push([v, t])
13+
graph[v].push([u, t])
14+
}
15+
let res = 0, visited = Array(n).fill(false)
16+
bt(0, 0, 0)
17+
return res
18+
19+
function bt(i, cur, time) {
20+
if(time > maxTime) return
21+
const backup = visited[i]
22+
if(!visited[i]) {
23+
visited[i] = true
24+
cur += values[i]
25+
}
26+
27+
if(i === 0) {
28+
res = Math.max(res, cur)
29+
}
30+
31+
for(const [next, nextTime] of (graph[i] || [])) {
32+
bt(next, cur, time + nextTime)
33+
}
34+
visited[i] = backup
35+
}
36+
};
37+
38+
// another
39+
140
/**
241
* @param {number[]} values
342
* @param {number[][]} edges

0 commit comments

Comments
 (0)