Arithmetic Slices - Java Solution, DP 2ms


  • 0
    V
        public int numberOfArithmeticSlices(int[] A) {
            if( A.length < 3 ) return 0;
            int cnt = 1, max = 0;
            int diff = A[0]-A[1];
            for(int i = 2; i < A.length; i++) {
                if(A[i-1] - A[i] == diff)   max += cnt++;
                else {
                    /*reset count and diff */
                    diff = A[i-1] - A[i];
                    cnt = 1;
                }
            }
            return max;
        }
    

Log in to reply
 

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