Watch 8 video solutions for Maximum Path Quality of a Graph, a hard level problem involving Array, Backtracking, Graph. This walkthrough by Tejpratap Pandey has 2,236 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49 Output: 75 Explanation: One possible path is 0 -> 1 -> 0 -> 3 -> 0. The total time taken is 10 + 10 + 10 + 10 = 40 <= 49. The nodes visited are 0, 1, and 3, giving a maximal path quality of 0 + 32 + 43 = 75.
Example 2:
Input: values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30 Output: 25 Explanation: One possible path is 0 -> 3 -> 0. The total time taken is 10 + 10 = 20 <= 30. The nodes visited are 0 and 3, giving a maximal path quality of 5 + 20 = 25.
Example 3:
Input: values = [1,2,3,4], edges = [[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime = 50 Output: 7 Explanation: One possible path is 0 -> 1 -> 3 -> 1 -> 0. The total time taken is 10 + 13 + 13 + 10 = 46 <= 50. The nodes visited are 0, 1, and 3, giving a maximal path quality of 1 + 2 + 4 = 7.
Constraints:
n == values.length1 <= n <= 10000 <= values[i] <= 1080 <= edges.length <= 2000edges[j].length == 3 0 <= uj < vj <= n - 110 <= timej, maxTime <= 100[uj, vj] are unique.Problem Overview: You are given an undirected graph where each node has a value and each edge has a travel time. Starting from node 0, you can walk through the graph as long as the total travel time does not exceed maxTime. Nodes may be revisited, but each node’s value is counted only the first time you visit it. The goal is to return the maximum total value collected among all paths that start and end at node 0.
Approach 1: Brute Force Path Enumeration using DFS (Exponential Time, O(V) Space)
The straightforward strategy is to enumerate every valid path that starts at node 0 and does not exceed maxTime. Build an adjacency list for the graph and run a depth-first search. At each step, iterate over all neighbors, subtract the edge travel time, and continue exploring if time remains. Maintain a visited frequency array to track whether a node's value has already been counted. Whenever the DFS returns to node 0, update the global maximum path quality. This approach explores all feasible paths, so the time complexity is exponential in the number of reachable paths, while the recursion stack and visit tracking require O(V) space.
Approach 2: DFS with Backtracking and Value Tracking (Exponential Time with Pruning, O(V) Space)
The practical solution uses DFS with backtracking. Represent the graph using an adjacency list from the graph. During traversal, keep a visit counter array for nodes. When entering a node for the first time (visitCount[node] == 0), add its value to the current score. Recursively explore each neighbor if the remaining time after the edge cost is non‑negative. After returning from recursion, decrement the visit counter to backtrack and restore the previous state. Because revisits are allowed but values count only once, the visit counter is critical. You also update the answer whenever the current node is 0. This search effectively enumerates valid paths but avoids recomputation of path state through careful backtracking. Time complexity remains exponential in the number of feasible paths constrained by maxTime, while space complexity stays O(V) for recursion and tracking.
The algorithm relies heavily on techniques from backtracking and recursive traversal of a graph. Because the time limit restricts path length, the search depth stays manageable for the given constraints.
Recommended for interviews: DFS with backtracking is the expected solution. Interviewers want to see how you model the graph with an adjacency list, track node visits so values are counted once, and correctly restore state during recursion. Brute force path enumeration shows understanding of the problem space, but the backtracking version demonstrates stronger control over recursion and state management.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force DFS Path Enumeration | Exponential in number of valid paths | O(V) | Understanding the problem or verifying correctness on small graphs |
| DFS with Backtracking and Visit Counting | Exponential (bounded by maxTime and branching factor) | O(V) | Recommended solution for interviews and competitive programming |