We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1613176 commit 90bb981Copy full SHA for 90bb981
kotlin/134-Gas-Station.kt
@@ -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
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