Skip to content

Latest commit

 

History

History
42 lines (34 loc) · 1.01 KB

leetcode_134.md

File metadata and controls

42 lines (34 loc) · 1.01 KB

LeetCode Problems

134. Gas Station

class Solution {
    /*
        Calculation Logic
        #1. Traverse from n-index clockWise to n-index.
        - calc: sum of all (gas_i - cost_i)
        #2. If "calc" positive, can traverse.
        #3. Start-point is must be first-all-positive-calc
     */
    fun canCompleteCircuit(gas: IntArray, cost: IntArray): Int {
        var startPoint = 0
        var calc = 0
        var tempCalc = 0
        for (i in 0 until gas.size) {
            val diff = gas[i] - cost[i]
            calc += diff
            tempCalc += diff
            // if running-diff is negative,
            // assign next index as start-point and reset running-diff.
            if (tempCalc < 0) {
                startPoint = i + 1
                tempCalc = 0
            }
        }
        return if (calc < 0) -1 else startPoint
    }
}