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.

题目大意:给定一个数组,找出最大连续子数组乘积。

解题思路:

解法一、我是用动态规划来解,因为都是整数,还是比较简单,从前往后遍历一次,需要维护比较三个变量,当前最小值,当前最大值,全局最大值。为什么需要当前最小值呢?不是求最大乘积嘛,因为如果当前数是负的,并且当前最小值也是负的,那么乘积可能就是下一个最大值。

LeetCode官方题解递推公式:

Let us denote that:

f(k) = Largest product subarray, from index 0 up to k.
Similarly, g(k) = Smallest product subarray, from index 0 up to k.
Then, f(k) = max( f(k-1) * A[k], A[k], g(k-1) * A[k] )
g(k) = min( g(k-1) * A[k], A[k], f(k-1) * A[k] )
    public int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int res = nums[0], min = nums[0], max = nums[0];
for (int i = 1; i < nums.length; i++) {
int curr_min = min * nums[i];
int curr_max = max * nums[i];
min = min(curr_min, curr_max, nums[i]);
max = max(curr_min, curr_max, nums[i]);
res = max(res, min, max);
}
return res;
} int min(int a, int b, int c) {
int tmp = Math.min(a, b);
return Math.min(tmp, c);
} int max(int a, int b, int c) {
int tmp = Math.max(a, b);
return Math.max(tmp, c);
}

解法二,上面说了,数组里都是整数。

①假设没有0,那么相乘的绝对值都是一直扩大的。所以可以这样考虑,偶数个负数,那么最大乘积就是所有的乘起来;要处理的就是奇数个负数的情况,这时就考虑舍弃左边第一个负数以左的乘积,或舍弃右边第一个负数以右的乘积。

②我们这里是有0的,我们可以考虑0将给定的数组划分为几个子数组,然后用①中的方式比较这些子数组中最大的乘积。

Talk is cheap>>

    public int maxProduct2(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int max = Integer.MIN_VALUE;
int prod = 1;
for (int i = 0; i < nums.length; i++) {
prod *= nums[i];
max = Math.max(max, prod);
if (nums[i] == 0) {
prod = 1;
}
}
prod = 1;
for (int i = nums.length - 1; i >= 0; i--) {
prod *= nums[i];
max = Math.max(max, prod);
if (nums[i] == 0) {
prod = 1;
}
}
return max;
}

Maximum Product Subarray——LeetCode的更多相关文章

  1. 152. Maximum Product Subarray - LeetCode

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

  2. Maximum Product Subarray - LeetCode

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

  3. LeetCode Maximum Product Subarray(枚举)

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

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

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

  5. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  6. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

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

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

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

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

  9. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

随机推荐

  1. 未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0[已解决]

    在使用百度UEditor,不小心将Newtonsoft.Json,升级了,然后就报的一个错,说: 其他信息: 未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, ...

  2. Nodejs服务器端脚本

    首先是安装,安装很简单,下载一个msi文件后一路下一步,没有难度, 测试的时候,如果你发现你的环境变量里面没有自动添加进去,也可以进行手动添加环境变量 之后在命令窗口输入: 得到nodejs的版本就说 ...

  3. maven发布的资源文件到tomcat项目下

    问题:项目中有hibernate的资源文件,src/main/java和src/main/resources都有这些文件,当启动项目时发现出错.但是src/main/java已经修改好了, 经查tom ...

  4. MySQL数据库迁移详细步骤(转)

    ========================================================================================== 一.背景简介 == ...

  5. C++中extern关键字使用(转)

    参考文章:http://blog.csdn.net/sruru/article/details/7951019 chapter1.如何混合编译C语言和C++ 实际开发过程中,C++中会调用C与语言编写 ...

  6. 单选按钮 点击value值自动把单选按钮选中

    HTML 代码 <tr>       <td align="right">性别:</td>       <td><inputt ...

  7. WF学习笔记(一)

    -流程启动方式1: WorkflowInvoker.Invoke(new Workflow1()); -流程启动方式2: WorkflowApplication instance = new Work ...

  8. 兼容IE6的页面底部固定层CSS代码

    有时候当我们需要把一个元素固定在页面的某个部位,一般都是用css中的“position:fixed;”方法来解决,但是IE6不支持fixed,所以今天分享一个兼容IE6的页面底部固定层CSS代码.如下 ...

  9. MVC Unit Testing学习笔记

    MVC Unit Testing 参考文档: 1.http://www.asp.net/mvc/overview/testing 2.http://www.asp.net/mvc/tutorials/ ...

  10. php 详解spl_autoload_register()函数

    在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.c ...