All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0101_0200.s0134_gas_station.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0101_0200.s0134_gas_station

// #Medium #Top_Interview_Questions #Array #Greedy
// #2022_10_09_Time_545_ms_(70.18%)_Space_74.6_MB_(45.61%)

class Solution {
    fun canCompleteCircuit(gas: IntArray, cost: IntArray): Int {
        var sumGas = 0
        var sumCost = 0
        var curGas = 0
        var result = -1
        for (i in gas.indices) {
            curGas += gas[i] - cost[i]
            // re-calculate the starting point
            if (curGas < 0) {
                result = -1
                curGas = 0
            } else if (result == -1) {
                // set initial starting point
                result = i
            }
            sumGas += gas[i]
            sumCost += cost[i]
        }
        return if (sumGas < sumCost) {
            -1
        } else result
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy