Why answer different between local machine and OJ?


  • 0
    C
    class Solution {
    public:
        int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
            int i, j, loop;
            int r = 0;
            for(i = 0; i < gas.size(); ) {
                r = gas[i];
                j = i;
                loop = 0;
                while(loop < gas.size()) {
                    if(r < cost[j]) {
                        i += loop+1;
                        break;
                    }
                    loop ++;
                    r -= cost[j];
                    j = (j++) % gas.size();
                    r += gas[j];
                }
                
                if (loop == gas.size()) 
                    return i;
            }
            
            return -1;
        }
    };
    

    input [1, 2] [2, 1] OJ return -1....


Log in to reply
 

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