Incorrect solution has been accepted


  • 0
    S

    I was checking around to see different implementations and found this one:

    class Solution {
    public:
        int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
    
            int start = 0, min = 0, sum = 0;
    
            for(int i = 0; i < gas.size(); i++)
            {
                sum = sum < 0 ? gas[i] - cost[i] : gas[i] - cost[i] + sum;
    
                if(sum < 0) {
                    min = min < sum ? min : sum;
                    start = i + 1;
                }
            }
    
            return start == gas.size() || sum + min < 0 ? -1 : start;
    
        }
    };
    

    It is accepted but should not pass the following test case, which expects -1. The solution above returns 3.

    gas = [99, 98, 97, 100], cost = [100, 100, 100, 97]


  • 0

    Thanks for pointing this out, just added this test case.


Log in to reply
 

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