my normal thought


  • 0
    C
    public int canCompleteCircuit(int[] gas, int[] cost) {
            int n = gas.length;
            int i = 0;
            int tank = 0;
            int t = 0;
    
            for (i = 0; i < n + t; i++) {
                tank += gas[i % n];
                if (tank >= cost[i % n]) {
                    tank -= cost[i % n];
                    if (i == n + t - 1) {
                        return t;
                    }
                } else {
                    tank = 0;
                    if (i >= n-1) {
                        break;
                    }
                    t = (i + 1) % n;
                    continue;
                }
            }
            return -1;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.