Skip to content

Commit

Permalink
Kotlin: 134. Gas Station
Browse files Browse the repository at this point in the history
  • Loading branch information
MaratKhakim committed Sep 1, 2022
1 parent 1613176 commit 90bb981
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions kotlin/134-Gas-Station.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
fun canCompleteCircuit(gas: IntArray, cost: IntArray): Int {
var sum = 0
val n = gas.size

for (i in 0..n-1) {
sum += gas[i] - cost[i]
}

if (sum < 0)
return -1

var gasInTank = 0
var start = 0

for (i in 0..n-1) {
gasInTank += gas[i] - cost[i]
if (gasInTank < 0) {
start = i+1
gasInTank = 0
}
}

return if (gasInTank < 0) -1 else start
}
}

0 comments on commit 90bb981

Please sign in to comment.