Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d5b74eb

Browse files
committed
Solve 134. Gas Station
1 parent 7951b7d commit d5b74eb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎01/0134-Gas_Station.kt

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /