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 ...
随机推荐
- oracle入门(4)——少而常用的命令
[本文介绍] 本文将介绍使用oracle的常用命令,不是”大全“,但少而实用. 下面的命令都是在项目用到的才记录下来的,如果以后用到其他的,会不断更新. [命令介绍] 描述: 命令: [数据库] ...
- golang 常用的日期方法和时区的坑
import( "time" ) 1.获取当前时间 time.Now(),返回类型:time结构. 2.字符串转为日期 t, _ := time.ParseInLocation(& ...
- Python Static Method
How to define a static method in Python?Demo: #!/usr/bin/python2.7 #coding:utf-8 # FileName: test.py ...
- day8 一些字符转换和深浅拷贝 和枚举
# li =[11,22,33,44,55,66,77,88]# del li[-2::-5]# print(li)# # dic = {'k1':'barry','k2':'alex','name' ...
- Hadoop2.7.x中所有的DataNode都启动不了解决办法
参考:Hadoop集群所有的DataNode都启动不了解决办法说明现象:我自己出现这个问题的原因是:自己在namenode格式化之后创建了一些文件,然后重新执行了namenode格式化导致的. 现象就 ...
- js keyCode(键盘键码)
摘自:http://blog.csdn.net/dyllove98/article/details/8728657 * 网上收集的KeyCode值方便大家查找: keycode 8 = BackSpa ...
- react下将输入的汉字转化为拼音
1.首先需要一个简单的拼音和汉字对应的字典文件: /** * 收录常用汉字6763个,不支持声调,支持多音字,并按照汉字使用频率由低到高排序 */ var pinyin_dict_notone = { ...
- Python学习之:pycharm配置
最近需要做一些小工具,听说Python不错,就学习一下.工欲善其事必先利其器,一个好的IDE对于学习一门新知识是很有帮助的,边写代码边换IDE,纠结了几天,最终还是选择了pycharm,之前觉得不够好 ...
- Django QuerySet API
https://docs.djangoproject.com/en/2.1/ref/models/querysets/
- 配置OpenVpn使用证书和用户名密码双验证
修改OpenVpn的主配置文件 添加一下内容: [root@check1 openvpn]# tail -3 server.conf auth-user-pass-verify /etc/openvp ...