LeetCode 2065. Maximum Path Quality of a Graph

原题链接在这里:https://leetcode.com/problems/maximum-path-quality-of-a-graph/

题目:

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.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
  • All the pairs [uj, vj] are unique.
  • There are at most four edges connected to each node.
  • The graph may not be connected.

题解:

We could do DFS to traverse the graph. Weighted graph could be represented by Map<Integer, Map<Integer, Integer>> graph. The last integer is the weight.

traverse the graph with DFS. DFS state includes values, maxTime, graph, curNode, curTime, curQuality and visited.

When curTime > maxTime, then return.

Since a node could be revisited. Here we use int[] visited to count visited times.

When visited[curNode] == 0, it is the first time we see this node, accumlate the value to curQuality.

If the current node is 0, we need to upate the result.

Note: There could be isolated node like [1, 2, weight]. No node is connected to 0. Thus graph.get(curNode) could be null. It needs to use getOrDefault, with default value as empty neighbos.

Time Complexity: O(n + maxTime). n = values.length. DFS could take maximum time.

Space: (n + e). n = values.length. e = edges.length. 

AC Java:

 1 class Solution {
 2     int res = 0;
 3     public int maximalPathQuality(int[] values, int[][] edges, int maxTime) {
 4         int n = values.length;
 5         Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
 6         for(int [] e : edges){
 7             graph.putIfAbsent(e[0], new HashMap<>());
 8             graph.putIfAbsent(e[1], new HashMap<>());
 9             graph.get(e[0]).put(e[1], e[2]);
10             graph.get(e[1]).put(e[0], e[2]);
11         }
12         
13         dfs(values, maxTime, graph, 0, 0, 0, new int[n]);
14         return res;
15     }
16     
17     private void dfs(int [] values, int maxTime, Map<Integer, Map<Integer, Integer>> graph, int curNode, int curTime, int curQuality, int [] visited){
18         if(curTime > maxTime){
19             return;
20         }
21         
22         if(visited[curNode] == 0){
23             curQuality += values[curNode];
24         }
25         
26         if(curNode == 0){
27             res = Math.max(res, curQuality);
28         }
29         
30         visited[curNode]++;
31         for(Map.Entry<Integer, Integer> entry : graph.getOrDefault(curNode, new HashMap<>()).entrySet()){
32             dfs(values, maxTime, graph, entry.getKey(), curTime + entry.getValue(), curQuality, visited);
33         }
34         visited[curNode]--;
35     }
36 }

 

posted @ 2022-08-28 16:40  Dylan_Java_NYC  阅读(119)  评论(0编辑  收藏  举报