Problem
Submissions

Solution: Gas Station

Statement

Naive approach

The naive approach would be to choose each station as a starting point in the outer loop and try to visit every other station while maintaining the current gas level in the inner loop. This will check for every gas station to be the starting gas station from where we can complete a round trip clockwise.

The time complexity of the naive approach would be O(n2)O(n^2), since we are using a nested loop.

Optimized approach using the greedy pattern

The greedy approach is to keep track of the amount of gas in the tank and the total cost of the journey. If we find that we cannot complete the journey starting from the current gas station, we reset the starting point to the next gas station and continue from there. This means we are making locally optimal choices at each step to find a solution. Therefore, this approach is considered a greedy algorithm.

The logic of the above algorithm is given below:

  1. If the total cost of the journey is greater than the total amount of gas available at all the gas stations, then it is impossible to travel through all gas stations, so the function returns −1-1.

  2. We will iterate through the gas stations from the start. While iterating these stations, we will perform the steps below:

    • At each gas station, we will calculate the amount of current gas available. We’ll do this by subtracting the cost of the journey from the gas available at that station and adding it to the current gas available.

    • If the amount of currently available gas at any station becomes negative, we cannot travel further from the current station. Therefore, we reset the current_gas variable to 00 and start from the next gas station.

  3. Return the index of the gas station from where we can start our journey in such a way that we can travel through all gas stations and reach the starting gas station again.

The starting index will be the index of that gas station from which we can start our journey in such a way that we can travel through all gas stations and reach the starting gas station again.

Let’s look at the following illustration to get a better understanding of the solution:

Problem
Submissions

Solution: Gas Station

Statement

Naive approach

The naive approach would be to choose each station as a starting point in the outer loop and try to visit every other station while maintaining the current gas level in the inner loop. This will check for every gas station to be the starting gas station from where we can complete a round trip clockwise.

The time complexity of the naive approach would be O(n2)O(n^2), since we are using a nested loop.

Optimized approach using the greedy pattern

The greedy approach is to keep track of the amount of gas in the tank and the total cost of the journey. If we find that we cannot complete the journey starting from the current gas station, we reset the starting point to the next gas station and continue from there. This means we are making locally optimal choices at each step to find a solution. Therefore, this approach is considered a greedy algorithm.

The logic of the above algorithm is given below:

  1. If the total cost of the journey is greater than the total amount of gas available at all the gas stations, then it is impossible to travel through all gas stations, so the function returns −1-1.

  2. We will iterate through the gas stations from the start. While iterating these stations, we will perform the steps below:

    • At each gas station, we will calculate the amount of current gas available. We’ll do this by subtracting the cost of the journey from the gas available at that station and adding it to the current gas available.

    • If the amount of currently available gas at any station becomes negative, we cannot travel further from the current station. Therefore, we reset the current_gas variable to 00 and start from the next gas station.

  3. Return the index of the gas station from where we can start our journey in such a way that we can travel through all gas stations and reach the starting gas station again.

The starting index will be the index of that gas station from which we can start our journey in such a way that we can travel through all gas stations and reach the starting gas station again.

Let’s look at the following illustration to get a better understanding of the solution: