# 152. Maximum Product Subarray (1d-dp)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Meo2HTzHXySBIkwfwUU%2F-MeqwvMaWVyb9ft7qW9e%2Fimage.png?alt=media\&token=6f4f4124-1302-46c2-9f7b-d36a508c3dfd)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Meo2HTzHXySBIkwfwUU%2F-MeqwzVIic5NcYVu7-f_%2Fimage.png?alt=media\&token=81071d54-5d56-4044-bd4e-d841aa1bd99e)

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

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

#### idea -&#x20;

#### max( max product, min product, current value), &#x20;

#### min( max product, min product, current value),&#x20;

#### because of negative\*negative may generate max number!

最後取最大值

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mg-A5RmsW8gxjnSLn1Z%2F-Mg-CsybNB46nuemkg1a%2Fimage.png?alt=media\&token=58c78d9c-37bf-49fa-9077-c817be0defed)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mg-A5RmsW8gxjnSLn1Z%2F-Mg-DlEP_gsb_7_CCLj-%2Fimage.png?alt=media\&token=37116c8e-78c9-400f-bf99-47b430262e10)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mg-A5RmsW8gxjnSLn1Z%2F-Mg-DxweRM4oVaaNvZyR%2Fimage.png?alt=media\&token=a66cd9cf-bef6-400e-938e-9426730bc0a5)

time: O(n)

space: O(1)

```java
class Solution {
    public int maxProduct(int[] nums) {
        int max = nums[0];
        int min = nums[0];
        int res = nums[0];
        for (int i = 1; i < nums.length; i++) {
            int temp = max;
            max = Math.max(Math.max(nums[i]*max, nums[i]*min), nums[i]);
            min = Math.min(Math.min(nums[i]*temp, nums[i]*min), nums[i]);
            res = Math.max(res, max);
        }
        return res;
    }
}
```

## original DP

```java
class Solution {
    public int maxProduct(int[] nums) {
        // [-2,0,-1], max = 0, 
        // 2 3 -3 4  max = 6
        // 2 -3 -3 4 max = 72 , so when even neg, will have max value
        // 2 -3 [-3 4 -3 -2 1 -4] max =  36* 8
        int dpMax[] = new int[nums.length];
        int dpMin[] = new int[nums.length];

        dpMax[0] = nums[0];
        dpMin[0] = nums[0];
        int res = nums[0];

        for (int i = 1; i < nums.length; i++) {
            dpMax[i] = Math.max(Math.max(nums[i]*dpMax[i-1], nums[i]*dpMin[i-1]), nums[i]) ;
            dpMin[i] = Math.min(Math.min(nums[i]*dpMax[i-1], nums[i]*dpMin[i-1]), nums[i]);
            res = Math.max(res, dpMax[i]);
        }
        return res;
    }
}
```

then use variables, or like first one way

```java
class Solution {
    public int maxProduct(int[] nums) {
        int max = nums[0];
        int min = nums[0];
        int res = nums[0];

        for (int i = 1; i < nums.length; i++) {
            int newMax = Math.max(Math.max(nums[i]*max, nums[i]*min), nums[i]) ;
            int newMin = Math.min(Math.min(nums[i]*max, nums[i]*min), nums[i]);
            max = newMax;
            min = newMin;
            res = Math.max(res, max);
        }
        return res;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/dynamic-programming/152.-maximum-product-subarray.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
