Skip to content

Commit

Permalink
GO: 134. Gas Station
Browse files Browse the repository at this point in the history
  • Loading branch information
MaratKhakim committed Jul 28, 2022
1 parent 6848691 commit f94d709
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions go/134-Gas-Station.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
func canCompleteCircuit(gas []int, cost []int) int {
sumGas := 0
sumCost := 0

for idx, _ := range gas {
sumGas += gas[idx]
sumCost += cost[idx]
}

if sumGas < sumCost {
return -1
}

res := 0
total := 0

for idx, _ := range gas {
total += gas[idx] - cost[idx]

if total < 0 {
total = 0
res = idx + 1
}
}

return res
}

0 comments on commit f94d709

Please sign in to comment.