python | 98.72% faster, O(n) time, O(1) space
We need to consider only the sign and zero.\nP represents positive value, N  represets negative value, and Z represents zero.\n\nExamples:\n-  If nums = NN, the
We need to consider only the sign and zero.\n`P` represents `positive value`, `N`  represets `negative value`, and `Z` represents `zero`.\n\nExamples:\n-  If nums = `NN`, the answer is `2` because `negative * negative` becomes `positive`.\n\n- if nums = `PNPNPNP`, then the answer is `5` because `PNPNP` becomes `positive`.\n\n- if nums = `NNPZPNPNNPP`, then the answer is `5` because `PNNPP` becomes `positive`.\n\nAs you may notice, if `nCnt (number of N) is even`, the subarray product becomes `positive`.\n\n---\n\nLet\'s take a look the case `NNPZPNPNNPP`.\n\n```\n\u3000\u3000\u3000 N\u3000N\u3000P\u3000Z\npCnt\u30000\u30000\u30001\u30000\n\nnCnt\u30001\u30002\u30002\u30000\n```\n\nAt this time, the answer is `2` because `NN` becomes `positive`.\n`P` is also valid subarray, but it\'s shorter than `NN`.\n\nIf `Z`, all counters are resetted. Then we continue the process.\n\n```\n\u3000\u3000\u3000 P\u3000N\u3000P\u3000N\u3000N\u3000P\u3000P\npCnt\u30001\u30001\u30002\u30002\u30002\u30003\u30004\n\nnCnt\u30000\u30001\u30001\u30002\u30003\u30003\u30003\n```\n\nAt the end, `nCnt` is odd, but if we remove one `N`, we can make `positive subarrays`.\nSo, we can make `PNPN` and `PNNPP`.\n\n```\nP\u3000N\u3000P\u3000N\u3000N\u3000P\u3000P\n   \u3000\u3000\u3000\u2193\nP\u3000N\u3000P\u3000N\n\u3000\u3000\u3000 P\u3000N\u3000N\u3000P\u3000P\n```\n\nTo make above subarrays, we need to know `how many P before the first N` and `how many P after the last N`,\nbecase those `P` is also removed with the first/last `N`.\n\n![image](https://assets.leetcode.com/users/images/f200c151-0615-4344-a11a-7f5e38724808_1641781783.5524125.jpeg)\n\n```python\nclass Solution:\n    def getMaxLen(self, nums: List[int]) -> int:\n        pCnt = nCnt = lastPcnt = firstPcnt = 0\n        ans = 0\n        nums.append(0)\n        for num in nums:\n            if num == 0:\n                if (nCnt % 2 == 0):\n                    ans = max(ans, pCnt + nCnt)\n                else:\n                    # print("pCnt = {}, nCnt = {}, firstPcnt = {}, lastPcnt = {}".format(pCnt, nCnt, firstPcnt, lastPcnt))\n                    ans = max(ans, pCnt - min(firstPcnt, lastPcnt) + nCnt - 1)\n                firstPcnt = lastPcnt = pCnt = nCnt = 0\n            elif num < 0:\n                nCnt += 1\n                lastPcnt = 0\n            else:\n                if nCnt == 0:\n                    firstPcnt += 1\n                pCnt += 1\n                lastPcnt += 1\n        return ans\n```\n\n
[]