Skip to content

Latest commit

 

History

History
19 lines (19 loc) · 373 Bytes

134加油站.md

File metadata and controls

19 lines (19 loc) · 373 Bytes
func canCompleteCircuit(gas []int, cost []int) int {
    curSum := 0
    totalSum := 0
    start := 0
    for i := 0; i < len(gas); i++ {
        curSum += gas[i] - cost[i]
        totalSum += gas[i] - cost[i]
        if curSum < 0 {
            start = i + 1
            curSum = 0
        }
    }
    if totalSum < 0 {
        return -1
    }
    return start
}