Arithmetic Slices II - Subsequence Problem
Description
LeetCode Problem 446.
Given an integer array nums, return the number of all the arithmetic subsequences of nums.
A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
- For example, [1, 3, 5, 7, 9], [7, 7, 7, 7], and [3, -1, -5, -9] are arithmetic sequences.
- For example, [1, 1, 2, 5, 7] is not an arithmetic sequence.
A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array.
- For example, [2,5,10] is a subsequence of [1,2,1,2,4,1,5,10].
The test cases are generated so that the answer fits in 32-bit integer.
Example 1:
Input: nums = [2,4,6,8,10]
Output: 7
Explanation: All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]
Example 2:
Input: nums = [7,7,7,7,7]
Output: 16
Explanation: Any subsequence of this array is arithmetic.
Constraints:
- 1 <= nums.length <= 1000
- -2^31 <= nums[i] <= 2^31 - 1
Solution
Approach: DP over Pairs with a Map per Index
There are 2n subsequences, so we cannot list them. Instead, notice that an arithmetic subsequence is decided by its last element and its common difference, and it can always be extended by one more element with the same difference.
Count weak arithmetic subsequences first: those of length at least 2. Every pair is one, and adding an element to a weak sequence of length ≥ 2 gives a real one of length ≥ 3.
Let ending[i][diff] be the number of weak arithmetic subsequences that end at index i with difference diff. For each pair j < i with diff = nums[i] - nums[j]:
before = ending[j][diff]sequences end atjwith this difference. Appendingnums[i]to each gives a sequence of length ≥ 3, so addbeforeto the answer.- Add
before + 1toending[i][diff]: thebeforeextended sequences, plus the new pair(nums[j], nums[i]).
Why this counts each answer exactly once: a sequence of length ≥ 3 is added at the moment its last element nums[i] is attached to the sequence of its other elements ending at j, and that (j, i) pair is unique to it.
For [2,4,6]: ending[1][2] = 1 (the pair 2,4), then at i = 2, j = 1 adds ending[1][2] = 1 to the answer (2,4,6).
The usual bug is overflow of the difference: with values from −231 to 231 − 1, nums[i] - nums[j] does not fit in 32 bits, and a wrapped value can make two different differences look equal. Java and C++ use a 64-bit key.
Complexity: O(n2) time and O(n2) space for the maps, where n is the length of nums.
class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
n = len(nums)
# ending[i][diff]: number of subsequences of length >= 2 that end at i with common difference diff.
ending = [{} for _ in range(n)]
total = 0
for i in range(n):
for j in range(i):
diff = nums[i] - nums[j]
before = ending[j].get(diff, 0) # sequences ending at j that nums[i] can extend
total += before # each becomes a sequence of length >= 3
# The extended ones, plus the new pair (nums[j], nums[i]).
ending[i][diff] = ending[i].get(diff, 0) + before + 1
return total