LeetCode之Maximum Product Subarray

1.(原文)问题描述

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.

2.翻译

找最大的数组中的子数组乘积

 例如,给予的数组是[2,3,-2,4]
连续的字数组[2,3]有最大的乘积=6

3.解决思路分析

  肯定可以通过其他方式实现就是不断的循环遍历,可是这样的代价太大,时间复杂度很高,我们j可以转换下思路好好想想如何最高效的来解决这个问题。

既然是求最大乘积那么我们需要考虑的就是最大乘积出现的情况可能有哪些:很显然有两种,第一种就是最大的累积乘以一个正数那么我们获取了更大的值,

还有就是一个最小的累积的值乘以一个负数,那么也可能获取的是最大的值,因为我们只需要保存乘积的最大和最小值。我们可以把数组的第一个值作为

最大最小值的逻辑值来处理,然后进行下去;如果之前的最大和最小值同当前元素相乘之后,没有当前元素大(或小)那么当前元素就可作为新的起点。

4.实现过程

Solution.java

package MaximumSubArray;

public class Solution {

	 public int maxProduct(int[] A) {

		 if(A == null||A.length==0){
			 return 0;
		 }
		 int maxProduct = A[0];  //最大值的初始值
         int maxTemp   = A[0]; //累积的最大值
         int minTemp  = A[0];  //累积的最小值

         for(int i = 1;i < A.length;i++) {//从数组的第二个元素开始遍历

            int a = A[i]*maxTemp;
            int b = A[i]*minTemp;
            maxTemp = Math.max(Math.max(a,b), A[i]);//选取最大值的新起点
            minTemp = Math.min(Math.min(a,b), A[i]);//选取最小值的新起点
            maxProduct = Math.max(maxProduct, maxTemp);//更新最大值
         }
          return maxProduct;
	    }

}

 SolutionTest.java

package MaximumSubArray;

public class SolutionTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int []array1 = {0,2,3,5,6,8,0,9};
		int []array2 = {1,-2,-3,-5,6,8,0,9};
		int []array3 = {1,2,-3,5,6,8,0,9};
		int []array4 = {1,2,0,5,6,8,0,9};

		System.out.println(new Solution().maxProduct(array1));
		System.out.println(new Solution().maxProduct(array2));
		System.out.println(new Solution().maxProduct(array3));
		System.out.println(new Solution().maxProduct(array4));
	}

}

 5.有句话很挣钱,解决思路很重要,重要的是思想。

时间: 2024-08-30 15:34:29

LeetCode之Maximum Product Subarray的相关文章

【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

题目: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] 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 6. Example 2: Input: [-2,0,-1]

[C++]LeetCode: 96 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. 思路: 这道题和Maximum Subarray解法类似,维护两个变量来动

LeetCode 152. 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. 题目标签:Array, Dynamic Programming 题目给了我们一个nu

LeetCode – Refresh – Maximum Product Subarray

Similar to maximum sum subarray, but need a gmin to record the global minimum to handle negative number multiplication. 1 class Solution { 2 public: 3 int maxProduct(int A[], int n) { 4 int result = A[0], gMax = A[0], gMin = A[0]; 5 for (int i = 1; i

【Leetcode】Maximum Product Subarray

题目链接:https://leetcode.com/problems/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 t

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

C#解leetcode 152. 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. 分析:这个题目就是让你求连续的子数组的乘积的最大值. (1)在数组中没有0的情况下,

leetcode 之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. Subscribe to see which companies as