#446. Arithmetic Slices II - Subsequence

ยท

1 min read

https://leetcode.com/problems/arithmetic-slices-ii-subsequence/description/?envType=daily-question&envId=2024-01-07

var numberOfArithmeticSlices = function (nums) {

    let result = 0,
        dp = new Array(nums.length).fill(null).map(() => new Map)

    for (let i = 0; i < nums.length; i++) {
        for (let j = 0; j < i; j++) {
            const diff = nums[i] - nums[j]
            const count = (dp[j].get(diff) || 0) + 1

            dp[i].set(
                diff,
                (dp[i].get(diff) || 0) + count
            )

            result += count - 1
        }
    }

    return result

};

Another hard question although the question was clear as day, the answer isn't so clear for now. Especially the later part of the area within the loop.

ย