Beats only 6.83%? Is there a faster method?


  • 0
    H

    I can't understand why the runtime of this code beats only 6.83 % of all CPP submissions.. is there a faster way to implement this??

    '''
    class Solution {
    public:
    int numberOfArithmeticSlices(vector<int>& A) {
    int n = A.size();

        if (n < 3)
            return 0;
            
        int prevcount = 0;
        int totalcount = 0;
       
        // Arithmetic slices must be consecutive numbers
        for (int i = 2; i < n; ++i){
            
            if (A[i] - A[i-1] == A[i-1] - A[i-2]){
                prevcount++;
            }
            else prevcount = 0;
            totalcount+=prevcount;
        }
        
        
        return totalcount;
        
    }
    

    };
    '''


Log in to reply
 

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