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

Difficulty: Medium


Problem Description

There is a tree 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 in the capital city. Each city has a car, and 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.


Key Insights

  • The problem can be modeled as a tree traversal, specifically using Depth-First Search (DFS) or Breadth-First Search (BFS).
  • Each city has one representative that needs to reach the capital, and they can either drive directly or share a car.
  • The number of trips made to transport representatives to the capital is affected by the car capacity (seats).
  • The solution requires calculating how many representatives can be gathered at each node before proceeding to the capital.

Space and Time Complexity

Time Complexity: O(n) - Each node is visited once during the traversal. Space Complexity: O(n) - Space used by the call stack in DFS or for storing the adjacency list representation of the graph.


Solution

The problem can be solved using a Depth-First Search (DFS) approach. We can represent the cities and roads as an adjacency list. Starting from the capital (city 0), we will traverse the tree and calculate the number of representatives that need to travel from each subtree. For each node, the number of trips required to transport all its representatives to the capital is determined by dividing the total representatives by the number of seats in the car, rounding up to account for any remaining representatives. We then accumulate the total fuel cost as we return from each recursive call.


Code Solutions

def minimumFuelCost(roads, seats):
    from collections import defaultdict
    import math

    # Build the adjacency list for the tree
    graph = defaultdict(list)
    for a, b in roads:
        graph[a].append(b)
        graph[b].append(a)

    def dfs(node, parent):
        total_representatives = 0
        fuel_cost = 0
        
        for neighbor in graph[node]:
            if neighbor != parent:  # Avoid going back to the parent node
                reps_from_child, fuel_from_child = dfs(neighbor, node)
                total_representatives += reps_from_child
                fuel_cost += fuel_from_child
        
        # Add 1 for the representative in the current node
        total_representatives += 1
        
        # Calculate the number of trips needed and the fuel cost
        if node != 0:  # Don't count fuel cost for the capital city
            trips_needed = math.ceil(total_representatives / seats)
            fuel_cost += trips_needed
        
        return total_representatives, fuel_cost

    # Start DFS from the capital (node 0)
    _, total_fuel_cost = dfs(0, -1)
    return total_fuel_cost
← Back to All Questions