mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
add no.134 answer
This commit is contained in:
26
leetcode/0134.Gas-Station/Gas.go
Normal file
26
leetcode/0134.Gas-Station/Gas.go
Normal file
@ -0,0 +1,26 @@
|
||||
package leetcode
|
||||
|
||||
func canCompleteCircuit(gas []int, cost []int) int {
|
||||
totalGas := 0
|
||||
totalCost := 0
|
||||
currGas := 0
|
||||
start := 0
|
||||
|
||||
for i := 0; i < len(gas); i++ {
|
||||
totalGas += gas[i]
|
||||
totalCost += cost[i]
|
||||
currGas += gas[i] - cost[i]
|
||||
|
||||
if currGas < 0 {
|
||||
start = i + 1
|
||||
currGas = 0
|
||||
}
|
||||
}
|
||||
|
||||
if totalGas < totalCost {
|
||||
return -1
|
||||
}
|
||||
|
||||
return start
|
||||
|
||||
}
|
Reference in New Issue
Block a user