Skip to content

Commit

Permalink
gas station
Browse files Browse the repository at this point in the history
  • Loading branch information
sahil9510 committed Oct 19, 2022
1 parent a8efb1b commit d4b1c01
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions leetcode/GasStation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <vector>
using namespace std;

int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
// first of all we need to check whether we've sfficient fuel or not.
int total_cost = 0, total_fuel = 0, n = cost.size();
for (int i = 0; i < n; i++)
{
total_cost += cost[i];
total_fuel += gas[i];
}
// If the total fuel is lesser than the cost then definitely we can't cover the whole cicular tour.
if (total_fuel < total_cost)
{
return -1;
}

// If the total fuel is sufficient enough to cover the circular tour then definitely an answer exists
int curr_fuel = 0, start = 0; // start with zero fuel.
for (int i = 0; i < n; i++)
{
// If at any point our balance/ current fuel is negative that means we can't come to the i'th petrol pump from the previous pump beacuse our fuel won't allow us to cover such distance.
// So we'll make the i'th pump as the start point ans proceed. Simultaneously we'll make the current fuel to be 0 as we're starting freshly.
if (curr_fuel < 0)
{
start = i;
curr_fuel = 0;
}
// at any station we'll fill petrol and pay the cost to go to the next station . so current fuel would be the following.
curr_fuel += (gas[i] - cost[i]);
}
return start;
}

0 comments on commit d4b1c01

Please sign in to comment.