> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/dynamic-programming/152.-maximum-product-subarray.md).

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

![](/files/-MeqwvMaWVyb9ft7qW9e)

![](/files/-MeqwzVIic5NcYVu7-f_)

#### 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!

最後取最大值

![](/files/-Mg-CsybNB46nuemkg1a)

![](/files/-Mg-DlEP_gsb_7_CCLj-)

![](/files/-Mg-DxweRM4oVaaNvZyR)

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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
