leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

Gas station python solution is wrong

Open sickerin opened this issue 1 year ago • 1 comments

https://github.com/neetcode-gh/leetcode/blob/main/python/0134-gas-station.py

The current solution will fail this test case

chrome_h8Skr2Vyn8

The original solution https://github.com/neetcode-gh/leetcode/commit/a5ed6087b9ef4ef0455d933e47c50a05b538cfa1 in the repo doesn't work, someone submitted a fix, but another commit reverted a working solution https://github.com/neetcode-gh/leetcode/commit/3fb65a3ab9b87f038800e3878ae124b9c033d83a, back to the original failing one.

The solution in the video is this, but seems this was never committed

chrome_HnM7HpiBmi
class Solution:
    def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
        if sum(gas) < sum(cost):
            return -1
            
        total = 0
        res = 0
        for i in range(len(gas)):
            total += (gas[i] - cost[i])

            if total < 0:
                total = 0
                res = i + 1

        return res

Related issue https://github.com/neetcode-gh/leetcode/issues/2751

sickerin avatar Feb 15 '24 15:02 sickerin