Skip to main content

Minimum Fuel Cost to Report to the Capital - Solution & Explanation

MediumTreeDepth-First SearchBreadth-First SearchGraph26 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

Problem Statement

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 <= 105
  • roads.length == n - 1
  • roads[i].length == 2
  • 0 <= ai, bi < n
  • ai != bi
  • roads represents a valid tree.
  • 1 <= seats <= 105

Approach Overview

Problem Overview: You are given n cities connected by n-1 roads forming a tree. Each city has one representative who must travel to the capital (city 0). Cars have seats capacity and consume one unit of fuel per road traveled. Representatives can share cars along the way. The task is to compute the minimum total fuel required for everyone to reach the capital.

Approach 1: DFS to Aggregate Representatives (O(n) time, O(n) space)

Treat the road network as a tree rooted at the capital. Use Depth-First Search to traverse from the capital and compute how many representatives exist in each subtree. Every node contributes one representative. When returning from a child subtree, calculate the number of cars required to move those representatives to the parent using ceil(representatives / seats). Each of those cars consumes one unit of fuel for the edge to the parent. Summing this value across all edges gives the minimum fuel. The key insight is that representatives naturally merge while moving upward, so the optimal strategy is always to combine them as early as possible in the tree.

Approach 2: BFS with Leaf Processing (O(n) time, O(n) space)

Build an adjacency list and track the degree of each node in the graph. Start from leaf nodes (excluding the capital) and process them using a queue with Breadth-First Search. Each leaf sends its representatives to its parent, requiring ceil(count / seats) cars for that edge. After processing, add its representative count to the parent and reduce the parent’s degree. When the parent becomes a leaf, push it into the queue. This bottom-up pruning approach effectively collapses the tree while accumulating the required fuel cost.

Recommended for interviews: The DFS aggregation approach is the most common solution. It directly models the tree structure and makes the representative merging logic clear. BFS leaf-pruning reaches the same result but is slightly less intuitive. Showing the DFS version first demonstrates strong understanding of tree traversal and subtree aggregation, which interviewers typically expect for this problem.

Approach 1: DFS to Compute Minimum Fuel Cost

Using 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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor β†’

Approach 2: BFS to Compute Minimum Fuel Cost

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) based on visiting each node.
Space Complexity: O(n) for queue storage.

Try this approach in the editor β†’

Approach 3: Greedy + DFS

According to the problem description, we can find that all cars will only drive towards the capital (node 0).

Suppose there is a node a, its next node is b, and node a needs to pass through node b to reach the capital. In order to make the vehicles (fuel consumption) of node a as small as possible, we should greedily let the vehicles of the child nodes of node a converge to node a first, and then distribute the vehicles according to the number of seats seats. The minimum number of vehicles (fuel consumption) needed to reach node b is \lceil \frac{sz}{seats} \rceil. Where sz represents the number of nodes in the subtree with node a as the root.

We start a depth-first search from node 0, using a variable sz to count the number of nodes in the subtree with the current node as the root. Initially, sz = 1, representing the current node itself. Then we traverse all the child nodes of the current node. For each child node b, we recursively calculate the number of nodes t in the subtree with b as the root, and add t to sz, and then we add \lceil \frac{t}{seats} \rceil to the answer. Finally, return sz.

The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor β†’

Complexity Comparison

ApproachComplexity
DFS to Compute Minimum Fuel Cost

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.

BFS to Compute Minimum Fuel Cost

Time Complexity: O(n) based on visiting each node.
Space Complexity: O(n) for queue storage.

Greedy + DFSβ€”

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
DFS Subtree AggregationO(n)O(n)Most intuitive for tree problems; directly aggregates representatives from children
BFS Leaf PruningO(n)O(n)Useful when processing trees bottom-up using queue and node degrees

Video Solution

Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python β€’ NeetCodeIO β€’ 15,926 views views

Watch 9 more video solutions β†’

Frequently Asked Questions

Is Minimum Fuel Cost to Report to the Capital easy or hard?
The problem is classified as Medium on LeetCode. The graph structure is straightforward, but the challenge is recognizing that representatives can merge along the tree and that fuel cost should be calculated per edge using subtree counts.
Minimum Fuel Cost to Report to the Capital Python/Java solution
Most implementations build an adjacency list and run DFS from the capital. During traversal, each recursive call returns the number of representatives in that subtree. The algorithm calculates required cars using ceiling division and accumulates fuel cost. The same logic works in Python, Java, C++, C#, and JavaScript.
How to solve Minimum Fuel Cost to Report to the Capital in O(n)?
Model the roads as a tree rooted at city 0. Perform a DFS to count representatives in each subtree. When returning from a child node, compute the cars needed with ceil(subtree_representatives / seats) and add that to the fuel total. This bottom-up aggregation ensures representatives share cars as early as possible, giving the optimal O(n) solution.
What is the best approach for Minimum Fuel Cost to Report to the Capital?
The DFS subtree aggregation approach is typically considered the best solution. Traverse the tree from the capital and compute how many representatives exist in each subtree. For every edge to the parent, calculate the number of cars required using ceil(representatives / seats). This approach runs in O(n) time and O(n) space and cleanly models how representatives merge while moving toward the capital.
Is Minimum Fuel Cost to Report to the Capital asked at Google/Amazon/Meta?
Tree aggregation and graph traversal problems like this frequently appear in interviews at companies such as Amazon, Google, and Meta. The problem tests understanding of tree structures, DFS traversal, and resource aggregation across graph edges.
What data structure is used in Minimum Fuel Cost to Report to the Capital?
The solution primarily uses an adjacency list to represent the tree. DFS uses recursion and subtree counts, while the BFS alternative uses a queue and node degree tracking to process leaf nodes first.
What is the time complexity of Minimum Fuel Cost to Report to the Capital?
The optimal solution runs in O(n) time because each city and road in the tree is processed once. Both DFS and BFS approaches build an adjacency list and traverse the tree a single time. Space complexity is O(n) for storing the graph and recursion stack or queue.

Ready to solve this problem?

Practice Minimum Fuel Cost to Report to the Capital with our built-in code editor and test cases.

Practice on FleetCode