O(n) time and O(n) space C# solution with one pass.


  • 0
    A

    public class Solution {
    public int NumberOfArithmeticSlices(int[] A) {
    if(A.Length == 0 || A.Length == 1 || A.Length == 2) return 0;
    int [] c = new int[A.Length];
    int [] b = new int[A.Length];
    c[0] = 0;
    c[1] = 0;
    b[0] = 0;
    b[1] = 0;
    for(int i = 2; i < A.Length; i++)
    {
    if((A[i]- A[i-1]) == (A[i-1]- A[i-2]))
    {
    b[i] = b[i-1] + 1;
    c[i] = c[i-1] + b[i];
    }
    else{
    c[i] = c[i-1];
    b[i] = 0;
    }

        }
        return c[A.Length -1];
    }
    

    }


Log in to reply
 

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