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.

此题要求是要在一个无需的数组找出一个乘积最大的连续子数组

例如[2,3,-2,4],因为可能有负数,可以设置两个临时变量mint和maxt,mint存储遇到负数之后相乘变小的乘积,maxt用来存储大的乘积。

比如2*3=6,此时,mint = maxt = 6,当下次遇到-2时,mint = maxt = -12,此时乘积-12小于-2,则应使maxt = -2。为避免下个数还是负数,应使mint不变,若下次遇到负数,则乘积比maxt大,然后交换……

具体看代码:

 1 public class Solution {
 2     public int maxProduct(int[] A) {
 3         int n = A.length;
 4         int mint = 1;
 5         int maxt = 1;
 6
 7         int maxvalue = A[0];
 8         for(int i =  0 ; i < n ; i++){
 9             if(A[i] == 0){
10                 mint = 1;
11                 maxt = 1;
12                 if(maxvalue < 0)
13                     maxvalue = 0;
14             }else{
15                 int curmax = maxt * A[i];
16                 int curmin = mint * A[i];
17
18                 maxt = curmax > curmin ? curmax : curmin;
19                 mint = curmax > curmin ? curmin : curmax;
20
21                 if(maxt < A[i])
22                     maxt = A[i];
23
24                 if(mint > A[i])
25                     mint = A[i];
26
27                 if(maxt > maxvalue)
28                     maxvalue = maxt;
29             }
30         }
31
32         return maxvalue;
33
34     }
35 }
时间: 2024-12-13 18:51:41

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. 思路:类似最大和连续子序列那样,不过除了记录最大乘积,我们还要记录最小的乘积.这里我

lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列

题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题  法一:直接暴力求解 时间复杂度O(N2) public class Solution { /** * @param nums: an array of integers * @return: an integer */ public int maxProduct(int[] nums) { // write your c

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

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;

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

题目: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. 这道题属于动态规划的题型,