Java 3ms beats 95%, single pass, easy to read code, lots of comments
As noted in the code, this was adapted from the excellent solution by @cAbhi15 . It took me so long to grasp what they were doing, so I wanted to share the code
As noted in the code, this was adapted from the excellent solution by [@cAbhi15](https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2010274/Java-or-One-Pass-or-O(N)-or-Dynamic-Programming) . It took me so long to grasp what they were doing, so I wanted to share the code I wrote while trying to understand their strategy. This code is more verbose than their original, but it is still very fast. Also there are some microoptimizations that I added.\n\n```\n/*\nRuntime: 3 ms, faster than 95.28% of Java online submissions for Maximum Length of Subarray With Positive Product.\nMemory Usage: 58.4 MB, less than 90.01% of Java online submissions for Maximum Length of Subarray With Positive Product.\n*/\n\n// adapted from @cAbhi15 : https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2010274/Java-or-One-Pass-or-O(N)-or-Dynamic-Programming\n\nclass Solution {\n    public int getMaxLen(int[] nums) {\n        int n = nums.length;\n        \n        int curPositiveLen = nums[0] > 0 ? 1 : 0;\n        int curNegativeLen = nums[0] < 0 ? 1 : 0;\n        \n        int maxPositiveLen = curPositiveLen; // what we\'ll return\n        \n        for (int i = 1; i < n; ++i) {\n            \n            int curVal = nums[i];\n            \n            // case: curVal is negative\n            if (curVal < 0) {\n                \n                // subcase: at least one negative and zero or more positives in the subarray so far, and now\n                //      this negative `curVal`, so we have both positive and negative subarrays in progress.\n                if (curNegativeLen > 0) {\n                    // swap `curNegativeLen + 1` and `curPositiveLen + 1`\n                    int temp = curNegativeLen + 1;\n                    curNegativeLen = curPositiveLen + 1;\n                    curPositiveLen = temp;\n                \n                // subcase: all positives so far in the current subarray and now this negative `curVal`.\n                // so we have a negative subarray now, and no positive subarray anymore.\n                } else { // curNegativeLen == 0\n                    curNegativeLen = curPositiveLen + 1;\n                    curPositiveLen = 0;\n                }\n            }       \n            \n            // case: curVal is positive\n            else if (curVal > 0) {\n                ++curPositiveLen;  // the positive subarray gets longer\n                if (curNegativeLen > 0) { // an existing negative subarray also gets longer with a positive `curVal`\n                    ++curNegativeLen;\n                }\n            }\n            \n            // case: curVal is zero\n            else {\n                // reset both positive and negative subarrays\n                curPositiveLen = 0;\n                curNegativeLen = 0;\n            }\n            \n            // update our best result so far\n            maxPositiveLen = Math.max(maxPositiveLen, curPositiveLen);\n        }\n        \n        return maxPositiveLen;\n    }\n}\n```
['Two Pointers', 'Dynamic Programming', 'Greedy']