题目描述:

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.

分析:

这里主要得考虑两个因素:1、头元素到第一个0元素之间和两个0元素之间负数的个数;2、当遇到0时,应该重新计算当前的最大值

设计的思想:

1、本题为了更加方便,我使用了额外的空间,且空间复杂度为O(n)

2、第一个数组主要用来计算从头或者从0开始相乘的值,第二个数组是用来计算从头或者0后面第一个负数开始每个元素乘积。

例:(由于表达不好,上面可能解释的不是太清楚)

原始序列:

2

4

-1

3

4

0

2

-1

3

-1

oneValue:

2

4

-4

-12

-48

0

2

-2

-6

6

twoValue:

2

4

-4

3

12

0

2

-2

3

-3

代码:

package com.edu.leetcode;

import javax.xml.transform.Templates;

public class MaximumProductSubarray {

    public int maxProducts(int[] A) {
if(A.length==1){ //如果数组中只有一个元素直接输出
return A[0];
}
int[] oneValues = new int[A.length]; //辅助空间1,记录从头或者从0开始到当前位置的乘积
int[] twoValues=new int[A.length]; //辅助空间2,从头或者从0开始遇到第一负数时,后面的值重新从当前位置开始
oneValues[0]=A[0];
twoValues[0]=A[0];
boolean first =true; //从头或者从0开始,判断是否遇到了负数
for(int i=1;i<A.length;i++){
if(oneValues[i-1]==0){
oneValues[i]=A[i];
twoValues[i]=A[i];
first=true;
}
else{
if(A[i-1]<=-1&&first){ //当前面的一个数是从头或者从0开始的第一个负数时,将twoValues[i]的值赋值为A[i]
twoValues[i]=A[i];
oneValues[i]=oneValues[i-1]*A[i];
first=false;
}
else{
oneValues[i]=oneValues[i-1]*A[i];
twoValues[i]=twoValues[i-1]*A[i];
}
}
}
int maxValue=oneValues[0];
for(int i=0;i<oneValues.length;i++){
if(oneValues[i]>maxValue)
maxValue=oneValues[i];
if(twoValues[i]>maxValue)
maxValue=twoValues[i];
}
return maxValue;
} public static void main(String[] args) {
// TODO Auto-generated method stub
MaximumProductSubarray mps = new MaximumProductSubarray();
int[] s = {2,3,-1};
System.out.println(mps.maxProducts(s)); } }

Maximum Product Subarray JAVA实现的更多相关文章

  1. leetcode 152. Maximum Product Subarray --------- java

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

  2. 152. Maximum Product Subarray - LeetCode

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

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

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

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

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

  5. LeetCode Maximum Product Subarray(枚举)

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

  6. LeetCode_Maximum Subarray | Maximum Product Subarray

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

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

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

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

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

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

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

随机推荐

  1. CC150 上面重要的题目总结

    第一章 : 全部重要 (1.6, 1.7 Leetcode上有). 1.5 面A碰到 (string compression) 1.7面Z碰到 (set 0) 1.8面Bigfish碰到 (strin ...

  2. SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  3. 简单易懂的现代魔法——Play Framework攻略3

    接前文:http://www.cnblogs.com/Kassadin/p/4343682.html 目前为止,我们已经研究了Play Framework的体系结构以及Web应用程序的工作原理.本来今 ...

  4. linux入门教程(十) 文档的压缩与打包

    在windows下我们接触最多的压缩文件就是.rar格式的了.但在linux下这样的格式是不能识别的,它有自己所特有的压缩工具.但有一种文件在windows和linux下都能使用那就是.zip格式的文 ...

  5. [优先队列]HDOJ5360 Hiking

    题意:有n个人,每个人有两个参数$l$和$r$ 邀请他们去hiking, 当  当前已经邀请到的人数大于等于$l$,并且小于等于$r$,那么这个人就会去 问最多能邀请到几个人 并输出 依次要邀请的人的 ...

  6. 区间dp笔记√

    区间DP是一类在区间上进行dp的最优问题,一般是根据问题设出一个表示状态的dp,可以是二维的也可以是三维的,一般情况下为二维. 然后将问题划分成两个子问题,也就是一段区间分成左右两个区间,然后将左右两 ...

  7. *[hackerrank]Volleyball Match

    https://www.hackerrank.com/contests/w1/challenges/volleyball-match 此题不错,首先可以看出是DP,S(x, y)= S(x - 1, ...

  8. 【转】Windows Server 2008修改远程桌面连接数

    按照下面的设置是成功了的,我设置的连接数是5个. http://jingyan.baidu.com/article/154b463150d1b128ca8f4194.html

  9. python os.stat() 和 stat模块详解

    stat 系统调用时用来返回相关文件的系统状态信息的. 首先我们看一下stat中有哪些属性: >>> import os >>> print os.stat(&qu ...

  10. iOS 精确定时器

    Do I need a high precision timer? Don't use a high precision timer unless you really need it. They c ...