We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Minimum Fuel Cost to Report to the Capital

Number: 2568

Difficulty: Medium

Paid? No

Companies: Amazon, American Express, Microsoft, Apple


Problem Description

Given an undirected tree representing cities (nodes) connected by roads (edges) where city 0 is the capital, each city has one representative with a car that has a limited number of seats. Representatives can switch cars and share rides. The cost of each road traversal is one liter of fuel. The goal is to determine the minimum total liters of fuel required to bring all representatives to the capital, optimizing the grouping of representatives across vehicles.


Key Insights

  • The cities form a tree, so there is a unique path between any two cities.
  • A Depth-First Search (DFS) from the capital allows aggregation of representatives in each subtree.
  • For each node (except the capital), calculate the number of trips needed as ceil(number_of_representatives / seats).
  • The fuel cost contribution for each node is the number of trips needed on the edge toward its parent.
  • Use efficient integer arithmetic (i.e., (people + seats - 1) // seats) to perform ceiling division.

Space and Time Complexity

Time Complexity: O(n) - Each node and edge is visited once during DFS. Space Complexity: O(n) - For the recursion stack and the adjacency list representing the tree.


Solution

We use a DFS starting from the capital (node 0) to calculate the total number of representatives in each subtree. For each node (except the capital), we calculate the number of trips required to move its aggregated representatives to the parent node, using ceiling division to account for partially filled rides. Every trip adds one road traversal cost (one liter of fuel). By summing the trips for all appropriate nodes, the algorithm determines the minimum fuel cost.


Code Solutions

Below are implementations in Python, JavaScript, C++, and Java with line-by-line comments.

# Python solution for Minimum Fuel Cost to Report to the Capital

from collections import defaultdict

def minimumFuelCost(roads, seats):
    # Create an adjacency list for the graph.
    graph = defaultdict(list)
    for u, v in roads:
        graph[u].append(v)
        graph[v].append(u)
    
    total_fuel = 0  # Variable to track total fuel cost.
    
    # DFS to count the number of representatives in each subtree.
    def dfs(node, parent):
        nonlocal total_fuel
        reps = 1  # Start with the current node's representative.
        # Traverse all neighboring nodes except the parent.
        for neighbor in graph[node]:
            if neighbor == parent:
                continue
            reps += dfs(neighbor, node)
        # If not the capital, calculate the number of trips needed.
        if node != 0:
            trips = (reps + seats - 1) // seats  # Ceiling division.
            total_fuel += trips  # Add trips (each trip accounts for one road traversal).
        return reps

    dfs(0, -1)  # Start DFS from the capital.
    return total_fuel

# Example usage:
# print(minimumFuelCost([[0,1],[0,2],[0,3]], 5))
← Back to All Questions