Minimum Refueling Stops: Problem 8
Minimum Refueling Stops: Problem 8
Minimum Refueling Stops: Problem 8
Minimum
Refueling
Stops
Group IX
Muhammad Zain
Ali Haider
Muhammad Askari
1 Problem
2 Design Approach
Table of 3 Algorithm
contents 4 Correctness
5 Time Complexity
6 Implementation
1
Problem
Introduction & Explanation
Problem Introduction
There are N petrol stations along the road between the car's
current location and the town where the friends can stop to
acquire additional petrol.
Within the loop, there is a nested loop that iterates over the petrol stations
to find the one with the maximum petrol amount. The maximum number of
iterations for this nested loop is N (number of petrol stations), as it checks
each station to find the maximum petrol amount. Therefore, the time
complexity of the nested loop is O(N).
Considering that the nested loop is inside the main loop, the overall time
complexity of the code is the product of the time complexities of the two
loops: O(D * N)
6
Implementation
Code in C#
PetrolStopsMinimization Class
using System;
namespace DAAppt
{
class PetrolStopsMinimization
{
public static int MinimizePetrolStops(int D, int P, int N, int[] petrolStations)
{
int P_curr = P; // Current petrol amount
int stops = 0;
int curr_distance = D; //from town
while (curr_distance > 0)
{
// Find the petrol station within the remaining distance that provides the maximum petrol
amount
for (int i = 0; i < N; i++)
{
int distanceToStation = curr_distance - petrolStations[i];
maxPetrolStationIndex = i;
petrolStations[i] -= maxPetrol;
}
}
PetrolStopsMinimization Class
if (maxPetrolStationIndex == -1)
return -1;
P_curr += maxPetrol;
curr_distance -= petrolStations[maxPetrolStationIndex];
stops++;
}
Console.WriteLine("\nafter refueling");
Console.WriteLine("Current petrol: " + P_curr);
Console.WriteLine("Current distance: " + curr_distance);
}
return -1;
}
}
}
Main Class
using System;
namespace DAAppt
{
class Program
{
static void Main(string[] args)
{
int D = 100; // Distance from the town
int P = 30; //initial petrol amount
int N = 5; //no of petrol stations
int[] petrolStations = { 30, 20, 40, 10, 50}; // Petrol amounts at each station