public int canCompleteCircuit(int[] gas, int[] cost) {
        int start = 0, totalGas = 0, totalCost = 0, tank = 0;
        for (int i = 0; i < gas.length; ++i) {
            tank += (gas[i] - cost[i]);
            totalGas += gas[i];
            totalCost += cost[i];
            if (tank < 0) {
                // if A cannot reach B
                // any node inside A and B can not reach B
                tank = 0;
                start = i + 1;
            }
        }
        if (totalCost > totalGas)
            return -1;
        return start;
    }