码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Maximum Product Subarray

时间:2015-03-21 06:22:49      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

Similar to maximum sum subarray, but need a gmin to record the global minimum to handle negative number multiplication.

 1 class Solution {
 2 public:
 3     int maxProduct(int A[], int n) {
 4         int result = A[0], gMax = A[0], gMin = A[0];
 5         for (int i = 1; i < n; i++) {
 6             int tmp = gMax;
 7             gMax = max(max(gMax*A[i], gMin*A[i]), A[i]);
 8             gMin = min(min(tmp*A[i], gMin*A[i]), A[i]);
 9             result = max(gMax, result);
10         }
11         return result;
12     }
13 };

 

LeetCode – Refresh – Maximum Product Subarray

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4355080.html

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