2477. Minimum Fuel Cost to Report to the Capital LeetCode Solution
In this guide, you will get 2477. Minimum Fuel Cost to Report to the Capital LeetCode Solution with the best time and space complexity. The solution to Minimum Fuel Cost to Report to the Capital problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Fuel Cost to Report to the Capital solution in C++
- Minimum Fuel Cost to Report to the Capital solution in Java
- Minimum Fuel Cost to Report to the Capital solution in Python
- Additional Resources

Problem Statement of Minimum Fuel Cost to Report to the Capital
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2477. Minimum Fuel Cost to Report to the Capital LeetCode Solution in C++
class Solution {
public:
long long minimumFuelCost(vector<vector<int>>& roads, int seats) {
long ans = 0;
vector<vector<int>> tree(roads.size() + 1);
for (const vector<int>& road : roads) {
const int u = road[0];
const int v = road[1];
tree[u].push_back(v);
tree[v].push_back(u);
}
dfs(tree, 0, -1, seats, ans);
return ans;
}
private:
int dfs(const vector<vector<int>>& tree, int u, int prev, int seats,
long& ans) {
int people = 1;
for (const int v : tree[u])
if (v != prev)
people += dfs(tree, v, u, seats, ans);
if (u > 0)
// the number of cars needed = ceil(people / seats)
ans += (people + seats - 1) / seats;
return people;
}
};
/* code provided by PROGIEZ */
2477. Minimum Fuel Cost to Report to the Capital LeetCode Solution in Java
class Solution {
public long minimumFuelCost(int[][] roads, int seats) {
List<Integer>[] tree = new List[roads.length + 1];
for (int i = 0; i < tree.length; ++i)
tree[i] = new ArrayList<>();
for (int[] road : roads) {
final int u = road[0];
final int v = road[1];
tree[u].add(v);
tree[v].add(u);
}
dfs(tree, 0, -1, seats);
return ans;
}
private long ans = 0;
private int dfs(List<Integer>[] tree, int u, int prev, int seats) {
int people = 1;
for (final int v : tree[u])
if (v != prev)
people += dfs(tree, v, u, seats);
if (u > 0)
// the number of cars needed = ceil(people / seats)
ans += (people + seats - 1) / seats;
return people;
}
}
// code provided by PROGIEZ
2477. Minimum Fuel Cost to Report to the Capital LeetCode Solution in Python
class Solution:
def minimumFuelCost(self, roads: list[list[int]], seats: int) -> int:
ans = 0
tree = [[] for _ in range(len(roads) + 1)]
for u, v in roads:
tree[u].append(v)
tree[v].append(u)
def dfs(u: int, prev: int) -> int:
nonlocal ans
people = 1 + sum(dfs(v, u) for v in tree[u] if v != prev)
if u > 0:
# the number of cars needed
ans += int(math.ceil(people / seats))
return people
dfs(0, -1)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.