Skip to content

Commit

Permalink
Merge pull request neetcode-gh#989 from MaratKhakim/134-Gas-Station-kt
Browse files Browse the repository at this point in the history
Kotlin: 134. Gas Station
  • Loading branch information
Ahmad-A0 authored Sep 2, 2022
2 parents 40c55f2 + 90bb981 commit b389041
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 b389041

Please sign in to comment.