152. Maximum Product Subarray (1d-dp)

1. 其實有 0 或 奇數個neg 時應該要重新選擇起始點, 所以要跟 nums[i] 比較

2. 偶數個 neg 時可能會產生 max 值, 解法是紀錄 max 和 min value, 如果neg*neg 時就會產生 max 值

idea -

max( max product, min product, current value),

min( max product, min product, current value),

because of negative*negative may generate max number!

最後取最大值

time: O(n)

space: O(1)

original DP

then use variables, or like first one way

Last updated

Was this helpful?