Learn DSA
Two Pointers
Sliding Window
Intervals
Stack
Linked List
Binary Search
Heap
Depth-First Search
Breadth-First Search
Backtracking
Graphs
Dynamic Programming
Greedy Algorithms
Trie
Prefix Sum
Matrices
Gas Station
DESCRIPTION (credit Leetcode.com)
There are n gas stations along a circular route. You are given two integer arrays gas and cost of length n. At each gas station i, gas[i] represents the amount of gas you receive by stopping at this station, and cost[i] represents the amount of gas required to travel from station i to the next station. You begin the journey with an empty tank at one of the gas stations.
Write a function to return the starting gas station's index if you can travel around the circuit once in the clockwise direction; otherwise, return -1. Note that if there exists a solution, it is guaranteed to be unique. Also, you can only travel from station i to station i + 1, and the last station will lead back to the first station.
EXAMPLES
Input:
gas = [5,2,0,3,3] cost = [1,5,5,1,1]
Output:
3
Explanation:
Start at station 4 (index 3) and fill up with 3 units of gas. Your tank = 0 + 3 = 3 Travel to station 4 with 1 unit of gas, and fill up with 3 units of gas. Your tank = 3 - 1 + 3 = 5 Travel to station 0 with 1 unit of gas, and fill up with 5 units of gas. Your tank = 5 - 1 + 5 = 9 Travel to station 1 with 5 units of gas, and fill up with 2 units of gas. Your tank = 9 - 5 + 2 = 6 Travel to station 2 with 5 units of gas, and fill up with 0 units of gas. Your tank = 6 - 5 + 0 = 1 Travel back to station 3 with 1 unit of gas to complete the circuit. Therefore, return 3 as the starting index.
Solution
def canCompleteCircuit(gas, cost):if sum(gas) < sum(cost):return -1start, fuel = 0, 0for i in range(len(gas)):if fuel + gas[i] - cost[i] < 0:# can't reach next station:# try starting from next stationstart, fuel = i + 1, 0else:# can reach next station:# update remaining fuelfuel += gas[i] - cost[i]return start
Explanation
Walkthrough
def canCompleteCircuit(gas, cost):if sum(gas) < sum(cost):return -1start, fuel = 0, 0for i in range(len(gas)):if fuel + gas[i] - cost[i] < 0:# can't reach next station:# try starting from next stationstart, fuel = i + 1, 0else:# can reach next station:# update remaining fuelfuel += gas[i] - cost[i]return start
def canCompleteCircuit(gas, cost):if sum(gas) < sum(cost):return -1start, fuel = 0, 0for i in range(len(gas)):if fuel + gas[i] - cost[i] < 0:# can't reach next station:# try starting from next stationstart, fuel = i + 1, 0else:# can reach next station:# update remaining fuelfuel += gas[i] - cost[i]return start
Greedy Approach
def canCompleteCircuit(gas, cost):if sum(gas) < sum(cost):return -1start, fuel = 0, 0for i in range(len(gas)):if fuel + gas[i] - cost[i] < 0:# can't reach next station:# try starting from next stationstart, fuel = i + 1, 0else:# can reach next station:# update remaining fuelfuel += gas[i] - cost[i]return start
def canCompleteCircuit(gas, cost):if sum(gas) < sum(cost):return -1start, fuel = 0, 0for i in range(len(gas)):if fuel + gas[i] - cost[i] < 0:# can't reach next station:# try starting from next stationstart, fuel = i + 1, 0else:# can reach next station:# update remaining fuelfuel += gas[i] - cost[i]return start
Loading comments...