Simple C++ method with 3ms


  • 0
    H
    class Solution {
    public:
    	int maxProduct(vector<int>& nums) {
    		int lpos = 1;
    		int lneg = 1;
    		int res = nums[0];
    		for (auto i : nums) {
    			res = (i<0) ? max(lneg*i, res) : max(lpos*i, res);
    			if (i > 0) {
    				lpos = max(lpos*i, 1);
    				lneg = (lneg<0)? lneg*i:1;
    			}
    			else {// i<=0
    				int temp = lpos;
    				lpos = max(lneg*i, 1);
    				lneg = temp*i;
    			}
    			//cout << "res: " << res << ", lpos: " << lpos << ", lneg: " << lneg << endl;
    		}
    		return res;
    	}
    };
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.