source: https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/description/
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 fuel cost 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class Solution { public long minimumFuelCost(int[][] roads, int seats) { Map<Integer, List<Integer>> map = new HashMap<>(); for(int[] road: roads){ addEdge(map, road[0], road[1]); addEdge(map, road[1], road[0]); } dfs(map, 0, -1, seats, new HashSet<>()); return fuel; } long fuel; public long dfs(Map<Integer, List<Integer>> map, int node, int parent, int seats, Set<Integer> visited){ //initialize person by 1 as the node itself has one person long persons = 1; if(!visited.contains(node)){ if(map.containsKey(node)){ for(int child : map.get(node)){ if(parent!=child){ //total person at a node is person at a node plus all persons at the child nodes persons = persons + dfs(map, child, node, seats, visited); } } } //if the node is not 0 //then calculate the cost of fuel it takes to reach to that node if(node!=0){ fuel += Math.ceil((double) persons / seats); } visited.add(node); } return persons; } private void addEdge(Map<Integer, List<Integer>> map, int a, int b){ List<Integer> l = map.getOrDefault(a, new ArrayList<>()); l.add(b); map.put(a, l); } } |