Advertisement
l_garg

Untitled

Jul 15th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int maxProduct(vector<int>& nums) {
  4.         int n=nums.size();
  5.         int max_prod=nums[0];
  6.         for(int i=1; i<n; i++){
  7.             int imax=max_prod;
  8.             int imin=max_prod;
  9.             if(nums[i]<0){
  10.                 swap(imax, imin);
  11.             }
  12.             imax=max(nums[i]*imax, nums[i]);
  13.             imin=min(nums[i]*imin, nums[i]);
  14.             max_prod=max(max_prod, imax);
  15.         }
  16.         return max_prod;
  17.     }
  18. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement