0 ms O(n) time, O(1) space C solution using DP


  • 0
    C
    int numberOfArithmeticSlices(int* A, int ASize) {
        int prev = 0, curr, ans = 0;
        for(int i = 2; i < ASize; i++){
            curr = 0;
            if(A[i] - A[i-1] == A[i-1] - A[i-2]){
                curr = prev + 1;
            }
            prev = curr;
            ans = ans + curr;
        }
        return ans;
    }
    

Log in to reply
 

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