JS solution, O(N) run time and constant space


  • 0
    S
    /**
     * @param {number[]} A
     * @return {number}
     */
    var numberOfArithmeticSlices = function(A) {
    if (A.length < 3)
            return 0;
        var result = 0;
        var currentStartIndex = 0;
        for (var i = 1; i < A.length - 1; i++) {
            if (A[i] - A[i - 1] === A[i + 1] - A[i]) {
                result +=  i - currentStartIndex;
            }
            else {
                currentStartIndex = i;
            }
        }
    
        return result;
    };
    

Log in to reply
 

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