Skip to content

Commit 6073326

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

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
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 = new Array(n).fill(0)
16+
visited[0] = 1
17+
bt(0, values[0], 0)
18+
return res
19+
20+
function bt(i, val, time) {
21+
if(time > maxTime) return
22+
if(i === 0) res = Math.max(res, val)
23+
24+
for(const [next, nextTime] of (graph[i] || [])) {
25+
const nextVal = visited[next] > 0 ? val : val + values[next]
26+
visited[next]++
27+
bt(next, nextVal, time + nextTime)
28+
visited[next]--
29+
}
30+
}
31+
};
32+
33+
// another
34+
35+
136
/**
237
* @param {number[]} values
338
* @param {number[][]} edges

0 commit comments

Comments
 (0)