【LeetCode刷题Java版】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.

package com.liuhao.acm.leetcode;

/**
 * @author liuhao 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.
 *
 */
public class MaximumProductSubarray {

	public static int maxProduct(int[] A) {
		int len = A.length;

		if (len <= 0) {
			return 0;
		}

		if (len == 1) {
			return A[0];
		}

		int max = A[0];
		int min = A[0];
		int result = A[0];

		for (int i = 1; i < len; i++) {
			int max_temp = max * A[i];
			int min_temp = min * A[i];
			max = Math.max(A[i], Math.max(max_temp, min_temp));
			min = Math.min(A[i], Math.min(max_temp, min_temp));
			result = Math.max(result, max);
		}

		return result;
	}

	public static void main(String[] args) {
		int[] a = new int[] { -4,-3 };
		System.out.println(maxProduct(a));
	}

}
时间: 2024-11-05 18:56:34

【LeetCode刷题Java版】Maximum Product Subarray的相关文章

【LeetCode刷题Java版】Evaluate Reverse Polish Notation(计算逆波兰表达式)

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

【LeetCode刷题Java版】Reverse Words in a String

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". click to show clarification. Clarification: What constitutes a word? A sequence of non-space characters constit

【leetcode刷题笔记】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

【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

leetcode 刷题之路 83 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6. 输入一个整形数组,数组里有正数也有负数,求所有子数组的和的最大

刷题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;

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_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