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.

思路:

0.动态规划问题,和求最大连续和maximum
subarray
类似,但感觉比求最大连续和复杂的多

1.以0为分割元素获得一系列的区间

2.对每一个区间求最大值

3.具体到每一个区间,顺序查找一遍寻找最大的序列,逆序查找一遍寻找最大的序列,求顺序或逆序查找的最大值

4.注意:(tempCount1&1) == 1)可以节省好多时间,用%2==1就不行

代码:

public int maxProduct(int[] nums) {
		if (nums == null)
			return 0;
		if (nums.length == 0)
			return 0;
		if (nums.length == 1)
			return nums[0];
		List<Integer> list = new ArrayList<Integer>();
		list.add(-1);
		for (int i = 0; i < nums.length; i++) {//以0为分割元素获得一系列的区间
			if (nums[i] == 0)
				list.add(i);
		}
		list.add(nums.length);
		int len = list.size();
		int max = 0, tempMax = 0;
		for (int i = 0; i < len - 1; i++) {//对每一个区间求最大值
			tempMax = getMax(nums, list.get(i), list.get(i + 1));
			max = max > tempMax ? max : tempMax;
		}
		return max;
	}

	public int getMax(int nums[], int start, int end) {
		if (end - start <= 2)//==2时说明区间除了0外只有一个元素,<2说明区间只有一个元素0
		{
			if(start!=nums.length-1)
				return nums[start + 1];
			else
				return nums[start];//最后一个元素
		}
		int sum = 1;
		int count = 0;
		for (int i = start + 1; i < end; i++) {
			sum *= nums[i];
			if (nums[i] < 0)
				count++;
		}
		int tempSum = sum;
		int tempCount = count;
		for (int i = start + 1; i < end; i++) {//顺序查找一遍寻找最大的序列
			if (tempSum < 0) {
				if (nums[i] < 0 &&(tempCount&1) == 1) {//(tempCount1&1) == 1)可以节省好多时间,用%2==1就不行
					tempSum /= nums[i];
					break;
				} else {
					tempSum /= nums[i];
				}
			} else
				break;
		}
		int tempSum1 = sum;
		int tempCount1 = count;
		for (int i = end - 1; i > start; i--) {//逆序查找一遍寻找最大的序列
			if (tempSum1 < 0) {
				if (nums[i] < 0 && (tempCount1&1) == 1) {//(tempCount1&1) == 1)可以节省好多时间,用%2==1就不行
					tempSum1 /= nums[i];
					break;
				} else {
					tempSum1 /= nums[i];
				}
			} else
				break;
		}
		int max = tempSum > tempSum1 ? tempSum : tempSum1;//求顺序或逆序查找的最大值
		return max;
	}
时间: 2024-11-10 22:11:24

leetcode_Maximum Product Subarray的相关文章

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】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. 题解: 先暴力解,遍历所有组合,更新最大值.很显然得超时. Solution

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】【Solution】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. [解法] 参考 http://segmentfault.com/blog

leecode 每日解题思路 152 Maximun Product Subarray

问题描述: 问题链接:152 Maximum Product Subarray 在经典的算法解析中, 有关的分治和动态规划的,经典题型之一就是求最大子段和, 这道题就是他的变形:求最大子段积; 这个问题的核心思路与解决最大子段和相同, 但是唯一需要注意的就是负数的情况. 每次在比较当前最大结果的同时,也需要保存当前最小结果,所以每个当前点i处的取值, 就是从当前值nums[i], 和cur_max+nums[i], cur_min+nums[i]三者中取极值: 每次的取值递推路径如上所示, 应该

leetcode-Maximum Product Subarray zz

LinkedIn 高频题 – Maximum Sum/Product Subarray Maximum Sum Subarray是leetcode原题,跟Gas Station的想法几乎一模一样.解答中用到的结论需要用数学简单地证明一下. 1 2 3 4 5 6 7 8 9 10 11 12 public int maxSubArray(int[] A) {     int sum = 0;     int max = Integer.MIN_VALUE;     for (int i = 0;

[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解法类似,维护两个变量来动