思路
- 参考: 当老司机学会了贪心算法
func canCompleteCircuit(gas []int, cost []int) int {
for i:=0; i < len(gas); {
sumOfGas, sumOfCost, cnt := 0, 0, 0
j := i % len(gas)
for cnt < len(gas) {
sumOfGas += gas[j]
sumOfCost += cost[j]
if sumOfGas < sumOfCost {
break
}
cnt++
j = (j+1)%len(gas)
}
if cnt == len(gas) {
return i
}else{
i = i + cnt + 1
}
}
return -1
}