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 ...
随机推荐
- JVM虚拟机—JVM的类加载机制
1 什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构 ...
- NHibernate & INotifyPropertyChanged
One of the things that make NHibernate easy to use is that it fully support the POCO model. But one ...
- 11 Spring框架 SpringDAO的JdbcTemplate
上几个章节我们探讨了Spring的IoC和AOP,这是Spring的重点,但是Spring对jdbc的支持同样我们也不能忘记,毕竟我们还要通过Spring来管理DAO框架(例如Hibernate或者M ...
- cdoj1337郭大侠与阴阳家
地址:http://acm.uestc.edu.cn/#/problem/show/1337 思路: 郭大侠与阴阳家 Time Limit: 3000/4000MS (Java/Others) ...
- 跟着实例学习ZooKeeper的用法: 分布式锁
锁 分布式的锁全局同步, 这意味着任何一个时间点不会有两个客户端都拥有相同的锁. 可重入锁Shared Reentrant Lock 首先我们先看一个全局可重入的锁. Shared意味着锁是全局可见的 ...
- 【Head First Servlets and JSP】笔记 25:JSTL 参考
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ t ...
- MySQL Binlog解析(2)
一.TABLE_MAP_EVENT Used for row-based binary logging beginning with MySQL 5.1.5.The TABLE_MAP_EVENT d ...
- maven项目在eclipse的library中没有Maven Dependencies
今天使用maven创建了一个多模块的项目,在分别创建完父项目和各个子模块后,编译父项目的时候,父项目工程目录上出现了一堆红叉叉,点进去一看,是找不到依赖的类,但是pom文件中相应jar的depende ...
- 关于ENABLE_BITCODE
pod 'TSVoiceConverter' 如果,设置了工程target的ENABLE_BITCODE为NO.但是,在真机上运行时,仍然提示类似于如下错误: URGENT: all bitcode ...
- Find Min In Rotated Sorted Array,寻找反转序列中最小的元素。
问题描述:寻找反转序列中最小的元素. 算法分析:和寻找某个数是一个道理,还是利用二分查找,总体上分两种情况.nums[left]<=nums[mid],else.但是,在截取子序列的时候,有可能 ...