Link: https://leetcode.com/problems/arithmetic-slices/
Solution:
Topics: greedy, subarray, reservoir
Intuition
Basically this is what I like to call a reservoir pattern. If an arithmetic slice contains N slices, then a newly added element creates exactly N+1 more slices. In other words, our current reservoir gets reabsorbed every time our slice grows (and plus one).
For example:
When our slice ends, we just set our current
variable to 0, signifying that we are no longer counting an arithmetic sequence.
Implementation
Visual
Review 1
Again, not reading the problem and assuming that I know what an arithmetic sequence is has bitten me in the ass. For whatever reason I thought I remembered this problem and falsely assumed that an arithmetic sequence is if 3 elements are increasing or decreasing by 1 (1,2,3,4
)…this is obviously not the case. An arithmetic sequence is any sequence that is increasing or decreasing consecutively by the same number for example:
1,2,3,4
2,4,6,8
100, 200, 300
-1, -2, -3
Other than the stupid oversight, I got the “reservoir” pattern correct.