There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of n cities numbered from 0 to n - 1 and exactly n - 1 roads. The capital city is city 0. You are given a 2D integer array roads where roads[i] = [ai, bi] denotes that there exists a bidirectional road connecting cities ai and bi.
There is a meeting for the representatives of each city. The meeting is in the capital city.
There is a car in each city. You are given an integer seats that indicates the number of seats in each car.
A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.
Return the minimum number of liters of fuel to reach the capital city.
Example 1:
Input: roads = [[0,1],[0,2],[0,3]], seats = 5 Output: 3 Explanation: - Representative1 goes directly to the capital with 1 liter of fuel. - Representative2 goes directly to the capital with 1 liter of fuel. - Representative3 goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed.
Example 2:
Input: roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 Output: 7 Explanation: - Representative2 goes directly to city 3 with 1 liter of fuel. - Representative2 and representative3 go together to city 1 with 1 liter of fuel. - Representative2 and representative3 go together to the capital with 1 liter of fuel. - Representative1 goes directly to the capital with 1 liter of fuel. - Representative5 goes directly to the capital with 1 liter of fuel. - Representative6 goes directly to city 4 with 1 liter of fuel. - Representative4 and representative6 go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed.
Example 3:
Input: roads = [], seats = 1 Output: 0 Explanation: No representatives need to travel to the capital city.
Constraints:
1 <= n <= 105roads.length == n - 1roads[i].length == 20 <= ai, bi < nai != biroads represents a valid tree.1 <= seats <= 105Using Depth-First Search (DFS), we can traverse the tree from the capital city '0' and calculate the minimum fuel cost required to gather all representatives in the capital city. We need to propagate the number of representatives from child nodes towards the root (capital) while managing the representatives getting into available seats effectively.
This C solution uses a depth-first search to calculate the minimum number of liters needed. It creates an adjacency list from the input nodes and applies DFS to compute two things: the total number of people at each node (along with its subtree) and the fuel cost for moving them to the capital.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the number of cities as we visit each edge once.
Space Complexity: O(n), due to the storage used for the adjacency list and recursion stack.
By utilizing Breadth-First Search (BFS), we can tackle this problem iteratively, processing nodes level by level from the capital outward. Track the representatives as they move towards the capital and the fuel cost incrementally.
This solution leverages BFS to maintain a queue for each city node. As nodes are processed, we calculate potential representatives from each node while incrementally updating fuel usage based on available seats. The BFS continues until all cities are visited.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) based on visiting each node.
Space Complexity: O(n) for queue storage.
| Approach | Complexity |
|---|---|
| DFS to Compute Minimum Fuel Cost | Time Complexity: O(n), where n is the number of cities as we visit each edge once. |
| BFS to Compute Minimum Fuel Cost | Time Complexity: O(n) based on visiting each node. |
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python • NeetCodeIO • 13,288 views views
Watch 9 more video solutions →Practice Minimum Fuel Cost to Report to the Capital with our built-in code editor and test cases.
Practice on FleetCode