Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Try to count the number of times each element occurred in a consecutive subsequence, then you can find the answer easily.
Think of dynamic programming as a solution to calculate the number in the previous hint.
Let <code>left_inc[i]</code> be the number of increasing consecutive subsequences ending at <code>nums[i]</code> (except for <code>nums[i]</code> itself).
Let <code>right_inc[i]</code> be the number of increasing consecutive subsequences starting at <code>nums[i]</code> (except for <code>nums[i]</code> itself).
Then <code>nums[i]</code> is in <code>left_inc[i] + right_inc[i] + left_inc[i] * right_inc[i] + 1</code> increasing subsequences.
Do the same for decreasing consecutive subsequences.