Given an array of integers, count the number of contiguous subsequences of alternating parity numbers, including length 1.
First approach (incorrect): array memo
Init memo arr
Traverse integer array [i... arr.length]
if not matching parity
memo[i] = memo[i - 1] + i + 1
else:
memo[i] = memo[i - 1] + 1
return memo[memo.length - 1]
Issue
Cannot increment memo[i] by i since it keeps increasing even when a subsequence is broken.
Fix
A separate pointer that resets to 1 whenever a contiguous subsequence is broken. Utilized to increment memo by the corresponding amount for its subsequence.
Optimization
Only using a prev pointer instead of an array since only last element is needed
VISA GCA