题目

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. ABP框架和NET CORE实战

    http://www.fishpro.com.cn/2017/09/ ABP实战系列 ABP实战 ABP-第一个Asp.net core 示例(7)AutoMapper的使用 我们为什么需要使用DDD ...

  2. JS——变量声明、变量类型、命名规范

    变量声明: JavaScript是一种弱类型语言,它的变量类型由它的值来决定,var是变量声明. 变量类型: 基本类型:number.string.boolean(布尔类型:var a=true/fa ...

  3. (转)Linux内核参数设置sysctl命令详解

    Linux内核参数设置sysctl命令详解 原文:https://www.zhukun.net/archives/8064 sysctl是一个允许您改变正在运行中的Linux系统的接口. 它包含一些 ...

  4. 《springcloud 三》分布式配置中心

    Git环境搭建 使用码云环境搭建git服务器端 码云环境地址:https://gitee.com/majie2018 服务端详解 项目名称:springboot2.0-config_server Ma ...

  5. [转] java实现https请求

    package com.lichmama.test.util; import java.io.ByteArrayOutputStream; import java.io.IOException; im ...

  6. Vue2.0 官方文档学习笔记

    VUE2.0官方文档 基础部分: 1.VUE简介 Vue是一个基于MVVM的框架,其中M代表数据处理层,V代表视图层即我们在Vue组件中的html部分,VM即M和V的结合层,处理M层相应的逻辑数据,在 ...

  7. <Android 应用 之路> 聚合数据SDK

    聚合数据介绍 聚合数据是一个为智能手机开发者,网站站长,移动设备开发人员及图商提供原始数据API服务的综合性云数据平台.包含手机聚合,网站聚合,LBS聚合三部分,其功能类似于Google APIS.[ ...

  8. Setting 之dashboard 点击跳转流程

    设置的主界面的可以通过修改xml中的dashboard_categaries.xml 文件实现,在DashboardSummary.java 文件中的rebuildUI()方法中将xml对应的实体类转 ...

  9. Android笔记--LayoutInflator源码和使用分析

    LayoutInflator源码分析 获取LayoutInflator对象 获取LayoutInflator的方式有两种: 使用LayoutInflator.from(Context context) ...

  10. 用Python完成根据日期计算是星期几

    import datetime def week(year,month,day): someday=dayetime.date(year,month,day) result={ "0&quo ...