Maximum Product Subarray JAVA实现

题目描述:

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));

    }

}
时间: 2024-10-09 18:28:00

Maximum Product Subarray JAVA实现的相关文章

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

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. 找出最大的相乘的数字,很简单,代码还可以优化的地方很多.但是速度还可以. publi

152. Maximum Product Subarray java solutions

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 pu

leetcode_152题——Maximum Product Subarray(动态规划)

Maximum Product Subarray Total Accepted: 33022 Total Submissions: 170218My Submissions Question Solution 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

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

Add Date 2014-09-23 Maximum Product Subarray 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 = 

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

LeetCode:Maximum Product Subarray

题目:Maximum Product Subarray 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: Maximum Product Subarray &amp;&amp; Maximum Subarray

Maximum Product Subarray Title: 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. 对于Product

152. Maximum Product Subarray(js)

152. Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product

刷题152. Maximum Product Subarray

一.题目说明 题目152. Maximum Product Subarray,给一列整数,求最大连续子序列,其乘积最大.难度是Medium! 二.我的解答 这个题目,用双重循环就可以了. class Solution{ public: int maxProduct(vector<int>& nums){ if(nums.size()<=1) return nums[0]; product = INT_MIN; int len = nums.size(); for(int i=0;