Best Python3 O(N) Solution 99.7% | Beginner Friendly | Detailed comments
Python3 O(n) solution\nDo not use DP, transform this problem into a String problem\n\nSteps:\n- split the given array by 0 elements (since 0 is not positive, it
### Python3 O(n) solution\nDo not use DP, transform this problem into a String problem\n\nSteps:\n- split the given array by 0 elements (since 0 is not positive, it will contaminate the sub-array);\n- calculate the max +product length for each sub-array from step 1;\n\t- I: if all positive, then we collect all elements;\n\t- II: if negative numbers are even, we also collect all elements;\n\t- III: if negative numbers are odd, we skip the first or the last of the number to find the maximum length (__skip the first/last negative elements__ means the remainning elements are garanteed as __case II__) \n\t- return the maximum length from above candidates;\n```\nclass Solution:\n    def getMaxLen(self, nums: List[int]) -> int:\n        leng = len(nums)\n        # sub: non zero sub-array derived from nums (use 0 as split point)\n        sub = []\n        # generate subarray\n        s = []\n        for i, n in enumerate(nums):\n            if n != 0:\n                s.append(n)\n            if n == 0:\n                sub.append(s)\n                s = []\n        sub.append(s)\n        \n        # get the maximum length of positive product for each sub array \n        # I: if all positive, then we collect all elements\n        # II: if negative numbers are even, we also collect all elements\n        # III: if negative numbers are odd, we skip the first or the last of the number to find the maximum length\n        \n        def max_leng(arr):\n            if all_positive(arr) or even_neg(arr):\n                return len(arr)\n            else:\n                return max(pick_left(arr), pick_right(arr))\n        \n        # return true if all the elements are above 0\n        def all_positive(arr):\n            for a in arr:\n                if a < 0:\n                    return False\n                \n            return True\n        # return true if the number of neg numbers are even\n        def even_neg(arr):\n            c = 0\n            for a in arr:\n                if a < 0:\n                    c += 1\n                    \n            return c % 2 == 0\n        # skip the first negative number, return the remaining length\n        def pick_left(arr):\n            for i, a in enumerate(arr):\n                if a < 0:\n                    return len(arr) - i - 1\n                    \n        # skip the last negative number, return the remaining length\n        def pick_right(arr):\n            arr.reverse()\n            for i, a in enumerate(arr):\n                if a < 0:\n                    return len(arr) - i - 1\n            \n        opt = []\n        # iterate all the sub-array to find the max length\n        for s in sub:\n            opt.append(max_leng(s))\n            \n        # return the max value \n        return max(opt)\n```\n\nThanks!
['String']