The idea is the same like Maximum Subarray, let $dp[i]$ to be the maximum product subarray value that ends at $nums[i]$, but we have some issues here:
- If $dp[i-1] = 3$, while the minimum product subarray value that ends at $i$ is $-4$ and $nums[i] = -1$, then we get $-4 \cdot -1 > 3 \cdot -1$
- Apparently we need to record the minimum product subarray as well, and we compare all the values.
$$ dp[i] = \max \lbrace dp_{min}[i-1] \cdot nums[i], dp_{max}[i-1] \cdot nums[i], nums[i] \rbrace $$
|
|