LeetCode OJ:Maximum Product Subarray(子数组最大乘积)
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.
很显然是一个动态规划的问题,找到递归表达式就可以了,考虑到会有负负相乘的问题,所以应该维持一个最大的值以及一个最小的值,递归表达式如下所示:
maxLocal[i + ] = max(max(maxLocal[i] * nums[i + ], minLocal[i] * nums[i + ]), nums[i + ]);
minLocal[i + ] = min(min(maxLocal[i] * nums[i + ], minLocal[i] * nums[i + ]), nums[i + ]);
maxGlobal[i + ] = max(maxGlobal[i], maxLocal[i + ]);
代码如下所示:
class Solution {
public:
int maxProduct(vector<int>& nums) {
int sz = nums.size();
if (sz == )return ;
if (sz == )return nums[];
int maxP, minP, a, b, ret;
ret = maxP = minP = a = b = nums[];
for (int i = ; i < sz; ++i){
a = max(maxP * nums[i], minP * nums[i]);
b = min(maxP * nums[i], minP * nums[i]);
maxP = max(a, nums[i]);
minP = min(b, nums[i]);
ret = max(maxP, ret);
}
return ret;
}
};
java版本的如下所示,注意使用两个中间变量的原因是防止maxVal先直接更新了之后又被minVal所误用:
public class Solution {
public int maxProduct(int[] nums) {
int maxVal, minVal, ret, tmpMax, tmpMin;
maxVal = minVal = ret = tmpMax = tmpMin = nums[0];
for(int i = 1; i < nums.length; ++i){
tmpMax = Math.max(maxVal * nums[i], minVal * nums[i]);
tmpMin = Math.min(maxVal * nums[i], minVal * nums[i]);
maxVal = Math.max(tmpMax, nums[i]);
minVal = Math.min(tmpMin, nums[i]);
ret = Math.max(maxVal, ret);
}
return ret;
}
}
LeetCode OJ:Maximum Product Subarray(子数组最大乘积)的更多相关文章
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- C#解leetcode 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode之Maximum Product Subarray
1.(原文)问题描述 Find the contiguous subarray within an array (containing at least one number) which has t ...
- [leetcode]152. Maximum Product Subarray最大乘积子数组
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetCode][001] Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- Java for LeetCode 152 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 152. Maximum Product Subarray --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- dplyr快速入门
RStudio Blog 介绍dplyr 包已发布 (Introducing dplyr), 此包将原本 plyr 包中的 ddply() 等函数进一步分离强化, 专注接受dataframe对象, 大 ...
- 初识ganglia
本文地址:http://www.cnblogs.com/qiaoyihang/ 一.Ganglia是什么?Ganglia主要用来解决什么样的问题? ganglia是一个可扩展的分布式监控系统,用于监控 ...
- HDU1081:To The Max(最大子矩阵,线性DP)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...
- HTML中的SEO和HTML语义化
SEO 1) <title>网站SEO标题</title>, 百度搜索出来的记录, 其标题基本就提取至网站的title, 标签, 因此标题起的好, 不论对点击率还是SEO都至关 ...
- 如何实现关系表的级联删除(ON DELETE CASCADE的用法)
以下面两张表为例: SQL> desc person 名称 是否为空? 类型 --------------------- ...
- 发布mvc3 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
今天在发布别人提供的MVC3的程序,正常部署后浏览报错,错误内容如图: 根据IIS提供的解决办法,启用目录浏览,刷新页面发现确实不报错了,但页面是以目录显示的,如图 虽然解决了报错问题,但不是正常的效 ...
- 【leetcode刷题笔记】Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 关于git bash的问题,pull不下来(登录之后,git帮你记住了,想切换其他用户)
参考博客: https://www.jianshu.com/p/8a7f257e07b8 从某个项目地址pull代码下来,老是报错 fatal: Authentication failed for ' ...
- 配置内核 Makefile:1449: *** mixed implicit and normal rules. Stop.【转】
本文转载自:https://blog.csdn.net/bitowang/article/details/8446494 在编译内核的时候提示Makefile:1449: *** mixed impl ...
- SQLServer行列转换PIVOT函数中聚合函数的使用意义及选择
例子:https://blog.csdn.net/wikey_zhang/article/details/76849826 DECLARE @limitDay INT;SET @limitDay = ...