题目

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.

Subscribe to see which companies asked this question

分析

最大字段积问题,之前我们熟悉的题目是求最大字段和。其实,他们本质是相同的,应该属于动态规划的范畴。

方法一(遗憾的TLE):

由于最终的最大乘积结果可能是从0~size-1的任何一个位置开始的子数组,所以我们可以先求出从0~siz-1开始的每个位置到最终位置的最大乘积,保存起来。

然后遍历该最大乘积数组,找出其中的最大值。

该方法复杂度有O(n^2)吧,遗憾的是,TLE了。。。

方法二:

不得不再次思考效率高的算法:

我们知道对序列元素遍历到 i 时,其值可正可负;

最大值可能是:

1. 前面子序列最大正乘积 * 正值;

2. 前面子序列最小负乘积 * 负值;

也就是说:其实子数组乘积最大值的可能性为:累乘的最大值碰到了一个正数;或者,累乘的最小值(负数),碰到了一个负数。所以每次要保存累乘的最大(正数)和最小值(负数)。

同时,还有一个选择起点的逻辑,如果之前的最大和最小值同当前元素相乘之后,没有当前元素大(或小)那么当前元素就可作为新的起点。例如,前一个元素为0的情况,{1,0,9,2},到9的时候9应该作为一个最大值,也就是新的起点,{1,0,-9,-2}也是同样道理,-9比当前最小值还小,所以更新为当前最小值。

TLE代码

class Solution {
public:
int maxProduct(vector<int>& nums) {
if (nums.empty())
return 0; int size = nums.size();
//求从0~size-1处开始,每处能够得到的最大子数组乘积
vector<int> maxP(size, 1);
for (int i = 0; i < size; ++i)
{
maxP[i] = nums[i];
}//for for (int i = 0; i < size; ++i)
{
int curP = maxP[i]; for (int j = i + 1; j<size; ++j)
{
curP *= nums[j];
if (maxP[i] < curP)
maxP[i] = curP;
}
}//for //找到最大子数组乘积中的最大乘积值
int maxRet = maxP[0];
for (int i = 1; i < size; ++i)
{
if (maxP[i] > maxRet)
maxRet = maxP[i];
}//for
return maxRet;
}
};

AC代码

class Solution {
public:
//方法一:遗憾的TLE
int maxProduct1(vector<int>& nums) {
if (nums.empty())
return 0; int size = nums.size();
//求从0~size-1处开始,每处能够得到的最大子数组乘积
vector<int> maxP(size, 1);
for (int i = 0; i < size; ++i)
{
maxP[i] = nums[i];
}//for for (int i = 0; i < size; ++i)
{
int curP = maxP[i]; for (int j = i + 1; j<size; ++j)
{
curP *= nums[j];
if (maxP[i] < curP)
maxP[i] = curP;
}
}//for //找到最大子数组乘积中的最大乘积值
int maxRet = maxP[0];
for (int i = 1; i < size; ++i)
{
if (maxP[i] > maxRet)
maxRet = maxP[i];
}//for
return maxRet;
} //方法二:时间复杂度为O(n)
int maxProduct(vector<int>& nums) {
if (nums.empty())
return 0; int size = nums.size(); //存储最大子数组乘积,当前最大、最小值
int maxRetP = nums[0], curMaxP = nums[0], curMinP = nums[0];
for (int i = 1; i < size; ++i)
{
int tmpMax = curMaxP * nums[i];
int tmpMin = curMinP * nums[i]; //更新当前最大、最小值
curMaxP = max(max(tmpMax , tmpMin), nums[i]);
curMinP = min(min(tmpMax , tmpMin), nums[i]); //更新当前最大子数组结果
maxRetP = max(maxRetP, curMaxP);
}//for
return maxRetP;
}
};

GitHub测试程序源码

LeetCode(152) Maximum Product Subarray的更多相关文章

  1. LeetCode(53) Maximum Subarray

    题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...

  2. LeetCode之“动态规划”:Maximum Product Subarray

    题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...

  3. LeetCode(164)Maximum Gap

    题目 Given an unsorted array, find the maximum difference between the successive elements in its sorte ...

  4. LeetCode(104) Maximum Depth of Binary Tree

    题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

  5. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  6. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  7. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  8. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  9. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

随机推荐

  1. Cache中间件和缓存降级

    Cache中间件和缓存降级 1.前言 surging受到不少.net同学的青睐,也提了不少问题,提的最多的是什么时候集成API 网关,在这里回答大家最近已经开始着手研发,应该在1,2个月内会有个初版A ...

  2. SPI接口的ETH芯片(ENC28J60/W5500)

    一 ENC28J60:SPI接口.中断.复位.LED指示.可参看野火相应教程.简单的在单片机中实现网页服务器是参考AVRNET项目,复杂的是用LWIP协议栈.telnet用于用PC的TELNET可以远 ...

  3. 求逆欧拉函数(arc)

    已知欧拉函数计算公式 初始公式:φ(n)=n*(1-1/p1)*(1-1/p2).....*(1-1/pm)   又 n=p1^a1*p2^a2*...*ps^as  欧拉函数是积性函数 那么:φ(n ...

  4. Java开发笔记(九十七)利用Runnable启动线程

    前面介绍了线程的基本用法,按理说足够一般的场合使用了,只是每次开辟新线程,都得单独定义专门的线程类,着实开销不小.注意到新线程内部真正需要开发者重写的仅有run方法,其实就是一段代码块,分线程启动之后 ...

  5. strust2的10种type类型

    <result-types> <result-type name="chain" class="com.opensymphony.xwork2.Acti ...

  6. JavaScript是什么

    JavaScript是一种解释型语言而不是编译型语言,它往往被认为是一种脚本语言,而不被看作是一种真正的编程语言.也就是说,脚本语言比较简单,它们是非程序员所使用的编程语言. 如果一个程序员对Java ...

  7. css hack 浏览器携带自身特有的属性 (二)

    css hack 浏览器携带自身特有的属性,才是我们真正要解决的css 兼容问题. 这里只是分享思路. 举例子: 1 outline,尤其是一些 自带继承特性的属性.这里指的是 隐性的inherite ...

  8. COGS 1619. [HEOI2012]采花

    ★★☆   输入文件:1flower.in   输出文件:1flower.out   简单对比时间限制:5 s   内存限制:128 MB [题目描述] 萧薰儿是古国的公主,平时的一大爱好是采花. 今 ...

  9. UVA12906 Maximum Score (组合)

    对于每个元素,最理想的情况就是都在它的左边或者右边,那么sort一下就可以得到一个特解了,然后大的中间不能有小的元素,因为如果有的话,那么无论选小的还是选大的都不是最优.对小的元素来说,比它大的元素在 ...

  10. 什么样子的WordPress网站更受搜索引擎欢迎

    网站的导航功能对于搜索引擎而言是非常重要的 网站的导航功能对于帮助用户迅速找到他们想要的内容来说是很重要的.它对帮助搜索引擎理解该网站有哪些重要内容同样非常重要.虽然百度的搜索结果都是指向每一个特定的 ...