This a task that asks u to compute the maximum product from a continue subarray. However, you need to watch

out the values' type contains positive, negative, zero.

I solved it using dynamic process in which there are two arrays to achieve the goal.

maxPro[i] : record the maximum product using nums[i] at end.

minPro[i] : record the minimum product using nums[i] at end.

Causing the neg and pos value types, we always can find the maximum product using the formula as below(maximum or minimum recording array):

  •   maxPro[i] = max3(nums[i], nums[i] * maxPro[i - 1], nums[i] * minPro[i - 1]);
  •   minPro[i] = min3(nums[i], nums[i] * maxPro[i - 1], nums[i] * minPro[i - 1]);
class Solution {
public:
int max3(int a, int b, int c){
return a > b? max(a, c): max(b, c);
} int min3(int a, int b, int c){
return a < b? min(a, c): min(b, c);
} int maxProduct(vector<int>& nums) {
vector<int>maxPro(nums.size(), );
vector<int>minPro(nums.size(), );
int ans = nums[];
for(int i = ; i < nums.size(); i ++){
if(i == ){
maxPro[] = nums[];
minPro[] = nums[];
}else{
maxPro[i] = max3(nums[i], nums[i] * maxPro[i - ], nums[i] * minPro[i - ]);
minPro[i] = min3(nums[i], nums[i] * maxPro[i - ], nums[i] * minPro[i - ]);
}
if(ans < maxPro[i]) ans = maxPro[i];
}
return ans;
}
};

最新文章

  1. scikit-learn 线性回归算法库小结
  2. clipboard_monitor_in_win7
  3. mac与php环境
  4. pushd
  5. 郝文菲get技能的经验--20155308
  6. 第二百三十八天 how can I 坚持
  7. POJ2492 A Bug&#39;s Life 带权并查集
  8. leetcode&mdash;triangle
  9. Eclipse:使用findBugs预先检测错误
  10. 两种解决Qt5显示中文乱码的方法(使用QStringLiteral和#pragma execution_character_set(&quot;utf-8&quot;)两种方法)
  11. 阐述linux IPC(两):基于socket进程间通信(下一个)
  12. 结构-行为-样式-JqueryUI拖放使用实例(全)
  13. java基本数据类型传递与引用传递区别
  14. Linux - rename 批量替换两种模式
  15. TVS(瞬间电压抑制器)
  16. tensorflow进阶篇-4(损失函数3)
  17. 解决Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 4
  18. Win10易升-在线升级工具
  19. 信号量 Linux函数 semget();semctl();semop();(转)
  20. Python 文件复制_bytes

热门文章

  1. [bzoj1878][SDOI2009][HH的项链] (莫队算法)
  2. [转] 探讨JS合并两个数组的方法
  3. Extended symmetrical multiprocessor architecture
  4. Nth Largest Value
  5. 转载 字符串hash
  6. - &gt; 动规讲解基础讲解三——混合背包(背包模板)
  7. vue2源码浏览分析01
  8. Visual Assist X破解版安装(vs2010助手)
  9. 转 java中5种创建对象的方法
  10. Tomcat PK Resin