Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int maxProduct(vector<int>& nums) {
- int n=nums.size();
- int max_prod=nums[0];
- for(int i=1, imax=max_prod, imin=max_prod; i<n; i++){
- // cout<<"i="<<i<<endl;
- // cout<<"imax="<<imax<<endl;
- // cout<<"imin="<<imin<<endl;
- if(nums[i]<0){
- swap(imax, imin);
- }
- imax=max(nums[i]*imax, nums[i]);
- imin=min(nums[i]*imin, nums[i]);
- max_prod=max(max_prod, imax);
- }
- return max_prod;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement