152. 乘积最大子数组

2021-01-18 13:16

阅读:408

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

class Solution {
  public:
  int maxProduct(vectorint>& 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 ) {
      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


评论


亲,登录后才可以留言!