result
to 0 to store the final count of arithmetic subsequences.dp
where dp[i]
is another dictionary that maps a common difference d
to the number of weak arithmetic subsequences ending at index i
with difference d
.i
and j
where i
goes from 1
to len(nums) - 1
and j
goes from 0
to i - 1
.(i, j)
, calculate the difference d = nums[i] - nums[j]
.d
is not in dp[i]
, initialize dp[i][d]
to 0.dp[j].get(d, 0) + 1
to dp[i][d]
. The get
method is used to handle the case when d
is not in dp[j]
.dp[j].get(d, 0)
to result
because these are the number of new arithmetic subsequences formed by adding nums[i]
.result
as the final answer.