码迷,mamicode.com
首页 > 编程语言 > 详细

152. 乘积最大子数组

时间:2020-05-18 22:24:53      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:target   class   pre   rev   tco   subarray   链接   div   ref   

class Solution {
  public:
  int maxProduct(vector<int>& nums) {
    int len = nums.size(), res = nums[0];
    int prevMin = nums[0], prevMax = nums[0];
    int temp1 = 0, temp2 = 0;
    for (int i = 1; i < len; i++) {
      temp1 = prevMin * nums[i];
      temp2 = prevMax * nums[i];
      prevMin = min(min(temp1, temp2), nums[i]);
      prevMax = max(max(temp1, temp2), nums[i]);
      res = max(prevMax, res);
    }
    return res;
  }
};

题目链接:https://leetcode-cn.com/problems/maximum-product-subarray/solution/

 

作者:hyj8
链接:https://leetcode-cn.com/problems/maximum-product-subarray/solution/wa-ni-zhe-ti-jie-shi-xie-gei-bu-hui-dai-ma-de-nu-p/
来源:力扣(LeetCode)
感谢!

152. 乘积最大子数组

标签:target   class   pre   rev   tco   subarray   链接   div   ref   

原文地址:https://www.cnblogs.com/rtyxxy/p/12913117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!