<p>Given an array of integers <code>nums</code>, find the maximum length of a subarray where the product of all its elements is positive.</p>

<p>A subarray of an array is a consecutive sequence of zero or more values taken out of that array.</p>

<p>Return <em>the maximum length of a subarray with positive product</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,-2,-3,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The array nums already has a positive product of 24.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,-2,-3,-4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest subarray with positive product is [1,-2,-3] which has a product of 6.
Notice that we cannot include 0 in the subarray since that&#39;ll make the product 0 which is not positive.</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,-2,-3,0,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The longest subarray with positive product is [-1,-2] or [-2,-3].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:


    int getNegativeMaxLenByIndex(vector<int>& nums, int index, vector<int>& dp, vector<int>& dp1) {
        if(index == 0) {
            if(nums[index] >= 0) return dp1[index] =  0;
            return dp1[index] =  1;
        }
        if(dp1[index] != -1) return dp1[index];

        int ans = 0;
        if(nums[index] > 0) {
            int tmp = getNegativeMaxLenByIndex(nums, index - 1, dp, dp1);
            if(tmp > 0)
                ans = max(ans, tmp + 1);
        } else if (nums[index] < 0) {
            int tmp = getPositiveMaxLenByIndex(nums, index - 1, dp, dp1);
            if(tmp > 0)
                ans = max(ans, tmp + 1);
        }

        if(ans == 0) {
            if(nums[index] < 0)
                return dp1[index] = 1;
            return dp1[index] = 0;
        }

        return dp1[index] = ans;
        
    }


    int getPositiveMaxLenByIndex(vector<int>& nums, int index, vector<int>& dp, vector<int>& dp1) {
        if(index == 0) {
            if(nums[index] > 0) return dp[index] = 1;
            return dp[index] = 0;
        }

        if(dp[index] != -1) return dp[index];

        int ans = 0;
        if(nums[index] > 0) {
            int tmp = getPositiveMaxLenByIndex(nums, index - 1, dp, dp1);
            if(tmp > 0)
                ans = max(ans, tmp + 1);
        } else if (nums[index] < 0) {
            int tmp = getNegativeMaxLenByIndex(nums, index - 1, dp, dp1);
            if(tmp > 0)
                ans = max(ans, tmp + 1);
        }

        if(ans == 0) {
            if(nums[index] > 0)
                return dp[index] = 1;
            return dp[index] = 0;
        }

        return dp[index] = ans;
    }

    int getMaxLen(vector<int>& nums) {
        int n = nums.size();

        vector<int> dp(n, -1);
        vector<int> dp1(n, -1);
        vector<int> dp2(n, 0);
        int ans = 0;
        
        for(int i = 0; i < n; i++) {
            getNegativeMaxLenByIndex(nums, i, dp, dp1);
            getPositiveMaxLenByIndex(nums, i, dp, dp1);
        }

        for(int i = 0; i < n; i++) {
            ans = max(ans, getPositiveMaxLenByIndex(nums, i, dp, dp1));
            cout << dp[i] << " " << dp1[i] << endl;
        }
        return ans;
    }
};
67500