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. 9.26 noip模拟试题

    魔术球问题弱化版(ball.c/.cpp/.pas) 题目描述 假设有 n 根柱子,现要按下述规则在这 n 根柱子中依次放入编号为 1,2,3,…的球. (1)每次只能在某根柱子的最上面放球. (2) ...

  2. 启动hadoop时候报错:localhost: ssh: Could not resolve hostname localhost: Temporary failure in name resolution”

    这个错误是由于配置文件没有配置好.解决方案如下: 1 打开profile文件 vim /etc/profile 2 在文件最后加入的内容应该如下(高亮的两句一般是大家缺少的): export JAVA ...

  3. ASP.NET Boilerplate Castle容器无缝添加日志功能

    以添加log4net日志框架为例进行讲解 1.通常log4net的配置参数放在单独的配置文件中,但也可以写在web.config中,这里在我们的web项目中添加log4net.config应用配置文件 ...

  4. 正则表达式匹配(node.js)

    参考文档如下: http://blog.csdn.net/huiguixian/article/details/6131048 http://www.91xueke.com/2013/04/05/30 ...

  5. (转)VS2012网站发布详细步骤

    2.弹出网站发布设置面板,点击<新建..>,创建新的发布配置文件: 4. 在配置中,要选择“Release”——发布模式(Release   称为发布版本,它往往是进行了各种优化,使得程序 ...

  6. Axure RP 8.0正式版下载地址 安装和汉化说明

    1.Axure RP和中文包包下载地址 官网地址:http://www.axure.com.cn/3510/ 2.下载完成后安装 3.破解 axure8.0注册码激活码:(亲测可用)用户名:aaa注册 ...

  7. cookie防篡改

    概述: 除了 session 外,一般不会在客户端的 cookies 里保存过于重要的凭据,但电商应用有时候不可避免地存储了一些敏感数据到客户端,当然不希望被篡改. 目的: 让服务器端能识别cooki ...

  8. UITableViewCell 上的按钮点击事件

     以前做tableViewCell的button点击事件,总是建立一个全局的可变数组,把data放在数组里,点击获取button的tag值,根据tag从数组了里取data. 其实,如果section只 ...

  9. 【转】UILabel、UITextView自适应得到高度

    原文:http://blog.csdn.net/xcysuccess3/article/details/8331549 在iOS中,经常遇到需要根据字符串的内容动态指定UILabel,UITextVi ...

  10. 十三、C# 事件

    1.多播委托 2.事件 3.自定义事件   在上一章中,所有委托都只支持单一回调. 然而,一个委托变量可以引用一系列委托,在这一系列委托中,每个委托都顺序指向一个后续的委托, 从而形成了一个委托链,或 ...