javalkak.blogg.se

Brute force ran out of gas but tank not empty
Brute force ran out of gas but tank not empty





brute force ran out of gas but tank not empty

If that’s not possible, repeat it by choosing another starting station. We will be discussing a brute force and a greedy solution for the problemĬheck if you can cover all the gas stations starting from any station. You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.Therefore, you can't travel around the circuit once no matter where you start.

brute force ran out of gas but tank not empty

Travel to station 1.Gas available in tank = 3 - 3 + 3 = 3 Travel to station 0.Gas available in tank = 4 - 3 + 2 = 3 Let's start at station 2 and fill up with 4 unit of gas. You can't start at station 0 or 1, as there is not enough gas to travel to the next station. Travel to station 3.The cost is 5.Your gas is just enough to travel back to station 3.Therefore, return 3 as the starting index. Travel to station 2.Gas available in tank = 6 - 4 + 3 = 5 Travel to station 1.Gas available in tank = 7 - 3 + 2 = 6 Travel to station 0.Gas available in tank = 8 - 2 + 1 = 7 Travel to station 4.Gas available in tank = 4 - 1 + 5 = 8 Start at station 3 (index 3) and fill up with 4 unit of gas. If you need to travel around the circuit once, otherwise return -1.īoth input arrays are non-empty and have the same length.Įach element in the input arrays is a non-negative integer.

brute force ran out of gas but tank not empty

At the beginning of the journey, the tank is empty at one of the gas stations. You have a car with an unlimited gas tank and it costs Gas stations along a circular route, where the amount of gas at station







Brute force ran out of gas but tank not empty