Skip to content

Commit 90bb981

Browse files
committed
Kotlin: 134. Gas Station
1 parent 1613176 commit 90bb981

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

kotlin/134-Gas-Station.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
fun canCompleteCircuit(gas: IntArray, cost: IntArray): Int {
3+
var sum = 0
4+
val n = gas.size
5+
6+
for (i in 0..n-1) {
7+
sum += gas[i] - cost[i]
8+
}
9+
10+
if (sum < 0)
11+
return -1
12+
13+
var gasInTank = 0
14+
var start = 0
15+
16+
for (i in 0..n-1) {
17+
gasInTank += gas[i] - cost[i]
18+
if (gasInTank < 0) {
19+
start = i+1
20+
gasInTank = 0
21+
}
22+
}
23+
24+
return if (gasInTank < 0) -1 else start
25+
}
26+
}

0 commit comments

Comments
 (0)