O(n) time O(1) space short DP C++ solution


  • 0
    H
    int numberOfArithmeticSlices(vector<int>& A) {
            
            if(A.size() < 3) return 0;
            
            int dp1 = 0;
            int dp2 = 0;
            int count = 0;
            
            for (int i = 3; i <= A.size(); i++) {
                dp2 = dp1 + ((A[i-2] - A[i-3]) == (A[i-1] - A[i-2]) ? ++count:count=0);
                dp1 = dp2;
            }
            
            return dp1;
        }
    

Log in to reply
 

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