Maximum Path Quality of a Graph - Problem

There is an undirected graph with n nodes numbered from 0 to n - 1 (inclusive). You are given a 0-indexed integer array values where values[i] is the value of the ith node. You are also given a 0-indexed 2D integer array edges, where each edges[j] = [uj, vj, timej] indicates that there is an undirected edge between the nodes uj and vj, and it takes timej seconds to travel between the two nodes.

Finally, you are given an integer maxTime.

A valid path in the graph is any path that starts at node 0, ends at node 0, and takes at most maxTime seconds to complete. You may visit the same node multiple times. The quality of a valid path is the sum of the values of the unique nodes visited in the path (each node's value is added at most once to the sum).

Return the maximum quality of a valid path.

Note: There are at most four edges connected to each node.

Input & Output

Example 1 — Basic Round Trip
$ Input: values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 25
Output: 53
💡 Note: Optimal path: 0 → 3 → 2 → 3 → 0. This visits unique nodes {0, 3, 2} with quality 0+43+10=53 in time 10+1+1+10=22 ≤ 25. Other paths like visiting both nodes 1 and 3 would require at least 40 time units (0→1→0→3→0 or 0→3→0→1→0), exceeding maxTime=25.
Example 2 — Just Stay at Start
$ Input: values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[2,3,10]], maxTime = 5
Output: 5
💡 Note: With only 5 time units, cannot visit any other node and return to 0 (each edge takes 10 time). Stay at node 0, quality = 5.
Example 3 — Quick Round Trip
$ Input: values = [1,2,3,4], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30
Output: 6
💡 Note: Can do 0 → 1 → 0 (quality = 1+2 = 3, time = 20) then 0 → 3 → 0 (adds +4, time +20 = 40 > 30). Or single trip 0 → 3 → 0 (quality = 1+4 = 5, time = 20). Or 0 → 1 → 0 → 3 → 0 would be time 40 > 30. Best single path: 0 → 1 → 0 = 3 (time 20), then can't do more. Wait, what about 0 → 1 → 2 → 1 → 0? Time = 10+10+10+10 = 40 > 30. Try 0 → 3 → 0 → 1 → 0? Time = 10+10+10+10 = 40 > 30. Actually, 0 → 1 → 0 → 3 → 0 impossible since we'd need 40 time but have 30. Best is either 0 → 1 → 0 (quality 3, time 20) or 0 → 3 → 0 (quality 5, time 20). Answer should be 5. But wait, we visit {0,3} so quality = values[0] + values[3] = 1 + 4 = 5. Hmm, let me reconsider the example. Maybe there's a better path I'm missing.

Constraints

  • n == values.length
  • 1 ≤ n ≤ 1000
  • 0 ≤ values[i] ≤ 108
  • 0 ≤ edges.length ≤ 2000
  • edges[j].length == 3
  • 0 ≤ uj < vj ≤ n - 1
  • 10 ≤ timej, maxTime ≤ 100
  • There are at most four edges connected to each node.

Visualization

Tap to expand
Maximum Path Quality of a Graph INPUT 0 val=0 1 val=32 2 val=10 3 val=43 t=10 t=15 t=10 values = [0, 32, 10, 43] edges = [[0,1,10], [1,2,15], [0,3,10]] maxTime = 25 ALGORITHM STEPS 1 Start DFS from node 0 Track time, visited nodes 2 Explore all neighbors If time + edge <= maxTime 3 Calculate quality Sum unique node values 4 Update max at node 0 Backtrack and try other Valid Paths Explored: 0 --> 1 --> 0 (t=20) q=32 0 --> 3 --> 0 (t=20) q=43 0 --> 1 --> 2 --> 1 --> 0 (t=50 > 25) SKIP 0 --> 3 --> 0 --> 1 --> 0 (t=30 > 25) SKIP FINAL RESULT Best Valid Path Found: 0 1 3 1 3 Path: 0 --> 3 --> 0 Time: 10 + 10 = 20 <= 25 OK Unique: {0, 3} Quality: 0 + 43 = 43 Output: 53 Key Insight: DFS explores all valid paths that start and end at node 0 within maxTime. Since each node has at most 4 edges and maxTime limits depth, the search space is bounded. Nodes can be revisited but their value only counts once. Best path: 0 --> 1 --> 0 --> 3 --> 0 takes exactly 40 > 25. So 0 --> 3 --> 0 --> 1 --> 0 also > 25. TutorialsPoint - Maximum Path Quality of a Graph | DFS Approach
Asked in
Google 15 Meta 12 Amazon 8
23.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen