Skip to content

Commit fda8d6a

Browse files
committed
leetcode
1 parent 0b1144f commit fda8d6a

4 files changed

Lines changed: 334 additions & 0 deletions

File tree

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
3+
4+
5+
-* 2477. Minimum Fuel Cost to Report to the Capital *-
6+
7+
8+
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.
9+
10+
There is a meeting for the representatives of each city. The meeting is in the capital city.
11+
12+
There is a car in each city. You are given an integer seats that indicates the number of seats in each car.
13+
14+
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.
15+
16+
Return the minimum number of liters of fuel to reach the capital city.
17+
18+
19+
20+
Example 1:
21+
22+
23+
Input: roads = [[0,1],[0,2],[0,3]], seats = 5
24+
Output: 3
25+
Explanation:
26+
- Representative1 goes directly to the capital with 1 liter of fuel.
27+
- Representative2 goes directly to the capital with 1 liter of fuel.
28+
- Representative3 goes directly to the capital with 1 liter of fuel.
29+
It costs 3 liters of fuel at minimum.
30+
It can be proven that 3 is the minimum number of liters of fuel needed.
31+
Example 2:
32+
33+
34+
Input: roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2
35+
Output: 7
36+
Explanation:
37+
- Representative2 goes directly to city 3 with 1 liter of fuel.
38+
- Representative2 and representative3 go together to city 1 with 1 liter of fuel.
39+
- Representative2 and representative3 go together to the capital with 1 liter of fuel.
40+
- Representative1 goes directly to the capital with 1 liter of fuel.
41+
- Representative5 goes directly to the capital with 1 liter of fuel.
42+
- Representative6 goes directly to city 4 with 1 liter of fuel.
43+
- Representative4 and representative6 go together to the capital with 1 liter of fuel.
44+
It costs 7 liters of fuel at minimum.
45+
It can be proven that 7 is the minimum number of liters of fuel needed.
46+
Example 3:
47+
48+
49+
Input: roads = [], seats = 1
50+
Output: 0
51+
Explanation: No representatives need to travel to the capital city.
52+
53+
54+
Constraints:
55+
56+
1 <= n <= 105
57+
roads.length == n - 1
58+
roads[i].length == 2
59+
0 <= ai, bi < n
60+
ai != bi
61+
roads represents a valid tree.
62+
1 <= seats <= 105
63+
64+
65+
66+
*/
67+
68+
import 'dart:collection';
69+
70+
class A {
71+
int ans = 0;
72+
int minimumFuelCost(List<List<int>> roads, int seats) {
73+
final List<List<int>> graph = List.filled(roads.length + 1, 0)
74+
.map((e) => List<int>.empty(growable: true))
75+
.toList();
76+
77+
for (int i = 0; i < graph.length; ++i) graph[i] = <int>[];
78+
79+
for (List<int> road in roads) {
80+
final int u = road[0];
81+
final int v = road[1];
82+
graph[u].add(v);
83+
graph[v].add(u);
84+
}
85+
86+
dfs(graph, 0, -1, seats);
87+
return ans;
88+
}
89+
90+
int dfs(List<List<int>> graph, int u, int prev, int seats) {
91+
int people = 1;
92+
for (final int v in graph[u]) {
93+
if (v == prev) continue;
94+
people += dfs(graph, v, u, seats);
95+
}
96+
if (u > 0)
97+
// # of cars needed = ceil(people / seats)
98+
ans += (people + seats - 1) ~/ seats;
99+
return people;
100+
}
101+
}
102+
103+
class B {
104+
List<List<int>> buildGraph(List<List<int>> roads, int seats) {
105+
List<List<int>> adj = List.empty().map((e) => List<int>.empty()).toList();
106+
for (int i = 0; i < seats; i++) adj.add(<int>[]);
107+
for (List<int> road in roads) {
108+
adj[road[0]].add(road[1]);
109+
adj[road[1]].add(road[0]);
110+
}
111+
return adj;
112+
}
113+
114+
int minimumFuelCost(List<List<int>> roads, int seats) {
115+
int size = roads.length + 1;
116+
List<List<int>> adj = buildGraph(roads, size);
117+
Queue<int> q = Queue();
118+
119+
//Storing total edges of a node using which we will traverse the tree,
120+
//If a non zero node have 1 edge then that node is kind of a leaf and we can take it
121+
List<int> edges = List.filled(size, 0);
122+
for (int i = 0; i < size; i++) {
123+
edges[i] = adj[i].length;
124+
if (i != 0 && edges[i] == 1) q.add(i);
125+
}
126+
127+
int fuel = 0;
128+
//Storing total person on each node initially 1 person is at every node
129+
List<int> totalMen = List.filled(size, 1);
130+
// Arrays.fill(totalMen,1);
131+
while (!q.isEmpty) {
132+
// remove
133+
int u = q.removeLast();
134+
//Traveling from u to v,
135+
//There will be only 1 valid node which will satisfy edges[v]>0 as u has only 1 valid edge
136+
for (int v in adj[u]) {
137+
// A visited node will have 0 edges as we are decreasing no of nodes for both src and dest,
138+
//after visiting each node
139+
if (edges[v] > 0) {
140+
int cars = totalMen[u] ~/ seats;
141+
if (totalMen[u] % seats != 0) cars++;
142+
// From node u to node v we need " petrol equal to no. of cars required
143+
fuel += cars;
144+
//All people from node u now have reached to node v, so to remember that we are
145+
//incrementing it
146+
totalMen[v] += totalMen[u];
147+
edges[v]--;
148+
edges[u]--;
149+
//Only add those nodes which are non-root and have become leaf now i.e.,
150+
//only on one way they can go(That path will eventually make it meet root node)
151+
if (v != 0 && edges[v] == 1) q.add(v);
152+
}
153+
}
154+
}
155+
156+
return fuel;
157+
}
158+
}
159+
160+
// DFS SUCKS so deep in DART
161+
class C {
162+
int fuelCost = 0;
163+
164+
int minimumFuelCost(List<List<int>> roads, int seats) {
165+
if (roads.length == 0) {
166+
return 0;
167+
}
168+
HashMap<int, List<int>> graph = HashMap();
169+
for (List<int> road in roads) {
170+
graph.putIfAbsent(road[0], () => <int>[]);
171+
graph[road[0]]?.add(road[1]);
172+
graph.putIfAbsent(road[1], () => <int>[]);
173+
graph[road[1]]?.add(road[0]);
174+
}
175+
this.fuelCost = 0;
176+
for (int city in graph[0] ?? {}) {
177+
numOfChildren(graph, city, 0, seats);
178+
}
179+
return this.fuelCost;
180+
}
181+
182+
int numOfChildren(
183+
HashMap<int, List<int>> graph, int city, int from, int seats) {
184+
if (graph[city]?.length == 1) {
185+
this.fuelCost += 1;
186+
return 1;
187+
}
188+
int children = 1;
189+
for (int child in graph[city] ?? {}) {
190+
if (child == from) {
191+
continue;
192+
}
193+
children += numOfChildren(graph, child, city, seats);
194+
}
195+
this.fuelCost += (children + seats - 1) ~/ seats;
196+
return children;
197+
}
198+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
func minimumFuelCost(roads [][]int, seats int) int64 {
4+
adjList := make([][]int, len(roads)+1)
5+
for i := range roads {
6+
a, b := roads[i][0], roads[i][1]
7+
adjList[a] = append(adjList[a], b)
8+
adjList[b] = append(adjList[b], a)
9+
}
10+
var fuel int64
11+
dfs(0, -1, adjList, seats, &fuel)
12+
return fuel
13+
}
14+
15+
func dfs(city int, parent int, adjList [][]int, seats int, fuel *int64) int {
16+
numNodesInSubtree := 1
17+
for _, nextCity := range adjList[city] {
18+
if nextCity == parent {
19+
continue
20+
}
21+
numNodes := dfs(nextCity, city, adjList, seats, fuel)
22+
numNodesInSubtree += numNodes
23+
(*fuel) += int64(numNodes / seats)
24+
if numNodes%seats != 0 {
25+
(*fuel)++
26+
}
27+
}
28+
return numNodesInSubtree
29+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# 🔥 DFS & BFS 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Code -1 Breath First Search
4+
5+
```dart
6+
class Solution {
7+
List<List<int>> buildGraph(List<List<int>> roads, int seats) {
8+
List<List<int>> adj = List.empty().map((e) => List<int>.empty()).toList();
9+
for (int i = 0; i < seats; i++) adj.add(<int>[]);
10+
for (List<int> road in roads) {
11+
adj[road[0]].add(road[1]);
12+
adj[road[1]].add(road[0]);
13+
}
14+
return adj;
15+
}
16+
17+
int minimumFuelCost(List<List<int>> roads, int seats) {
18+
int size = roads.length + 1;
19+
List<List<int>> adj = buildGraph(roads, size);
20+
Queue<int> q = Queue();
21+
22+
//Storing total edges of a node using which we will traverse the tree,
23+
//If a non zero node have 1 edge then that node is kind of a leaf and we can take it
24+
List<int> edges = List.filled(size, 0);
25+
for (int i = 0; i < size; i++) {
26+
edges[i] = adj[i].length;
27+
if (i != 0 && edges[i] == 1) q.add(i);
28+
}
29+
30+
int fuel = 0;
31+
//Storing total person on each node initially 1 person is at every node
32+
List<int> totalMen = List.filled(size, 1);
33+
34+
while (q.isNotEmpty) {
35+
// remove
36+
int u = q.removeLast();
37+
//Traveling from u to v,
38+
//There will be only 1 valid node which will satisfy edges[v]>0 as u has only 1 valid edge
39+
for (int v in adj[u]) {
40+
// A visited node will have 0 edges as we are decreasing no of nodes for both src and dest,
41+
//after visiting each node
42+
if (edges[v] > 0) {
43+
int cars = totalMen[u] ~/ seats;
44+
if (totalMen[u] % seats != 0) cars++;
45+
// From node u to node v we need " petrol equal to no. of cars required
46+
fuel += cars;
47+
//All people from node u now have reached to node v, so to remember that we are
48+
//incrementing it
49+
totalMen[v] += totalMen[u];
50+
edges[v]--;
51+
edges[u]--;
52+
//Only add those nodes which are non-root and have become leaf now i.e.,
53+
//only on one way they can go(That path will eventually make it meet root node)
54+
if (v != 0 && edges[v] == 1) q.add(v);
55+
}
56+
}
57+
}
58+
59+
return fuel;
60+
}
61+
}
62+
```
63+
64+
## Code -2 Depth First Search
65+
66+
```dart
67+
class Solution {
68+
int fuelCost = 0;
69+
70+
int minimumFuelCost(List<List<int>> roads, int seats) {
71+
if (roads.length == 0) {
72+
return 0;
73+
}
74+
HashMap<int, List<int>> graph = HashMap();
75+
for (List<int> road in roads) {
76+
graph.putIfAbsent(road[0], () => <int>[]);
77+
graph[road[0]]?.add(road[1]);
78+
graph.putIfAbsent(road[1], () => <int>[]);
79+
graph[road[1]]?.add(road[0]);
80+
}
81+
this.fuelCost = 0;
82+
for (int city in graph[0] ?? {}) {
83+
numOfChildren(graph, city, 0, seats);
84+
}
85+
return this.fuelCost;
86+
}
87+
88+
int numOfChildren(
89+
HashMap<int, List<int>> graph, int city, int from, int seats) {
90+
if (graph[city]?.length == 1) {
91+
this.fuelCost += 1;
92+
return 1;
93+
}
94+
int children = 1;
95+
for (int child in graph[city] ?? {}) {
96+
if (child == from) {
97+
continue;
98+
}
99+
children += numOfChildren(graph, child, city, seats);
100+
}
101+
this.fuelCost += (children + seats - 1) ~/ seats;
102+
return children;
103+
}
104+
}
105+
```

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ This repo contain leetcode solution using DART and GO programming language. Most
210210
- [**1162.** As Far from Land as Possible](AsFarFromLandAsPossible/as_far_from_land_as_possible.dart)
211211
- [**1129.** Shortest Path with Alternating Colors](ShortestPathWithAlternatingColors/shortest_path_with_alternating_colors.dart)
212212

213+
- [**2477.** Minimum Fuel Cost to Report to the Capital](MinimumFuelCostToReportToTheCapital/minimum_fuel_cost_to_report_to_the_capital.dart)
214+
213215
## Reach me via
214216

215217
[![Gmail](https://img.shields.io/badge/Gmail-D14836?style=for-the-badge&logo=gmail&logoColor=white)](https://ayoubzulfiqar3@gmail.com)

0 commit comments

Comments
 (0)