Posted on

Description

Submission

class Solution {
public:
    int getMaxLen(vector<int>& nums) {
        for(int i = 0; i < nums.size(); ++i) {
            if(nums[i] > 0) nums[i] = 1;
            if(nums[i] < 0) nums[i] = -1;
        }

        int leftPos = -1, leftNeg = -1, cur = 1;
        int ret = 0;
        for(int i = 0; i < nums.size(); ++i) {
            cur *= nums[i];
            if(cur == 0) {
                cur = 1;
                leftPos = i;
                leftNeg = -1;
                continue;
            }
            if(cur > 0 ) {
                if(leftPos == -1) ret = max(i - leftPos, ret);
                else ret = max(i - leftPos, ret);
            }

            if(cur < 0) {
                if(leftNeg == -1) leftNeg = i;
                else ret = max(i - leftNeg, ret);
            }

        }
        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *