1 Maximum Product Subarray_Leetcode

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.

Since two negative numbers may multiply and get a large number, I need to track the minimum number of each position in the sequence.

At each point, the maximum can be obtained by three numbers:

1. A[i]

2. A[i] * curmax

3. A[i] * curmin

Similar as the minimum update.

FIRST TRY ERROR:

I should use temp variable to store the curmax, since the update of curmin use the previous value of curmax.

Code:

class Solution {
public:
    int maxProduct(int A[], int n) {
        if(n == 0 || A == NULL) return 0;
        int curmax = A[0], curmin = A[0];
        int res = A[0];

        for(int i = 1; i < n; i++)
        {
            int tmpmax = curmax, tmpmin = curmin;   // take care, curmax is used when calc curmin, so do not update
            curmax = max(max(A[i], A[i]*tmpmax), A[i]*tmpmin);
            curmin = min(min(A[i], A[i]*tmpmax), A[i]*tmpmin);
            if(curmax > res) res = curmax;
        }
        return res;
    }
};

  

  

时间: 2024-10-14 16:07:24

1 Maximum Product Subarray_Leetcode的相关文章

Maximum Product of Word Lengths

Maximum Product of Word Lengths Total Accepted: 750 Total Submissions: 2060 Difficulty: Medium Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume tha

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 - 628 Maximum Product of Three Numbers

Leetcode - 628 Maximum Product of Three Numbers 628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]

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

UVa 11059 Maximum Product

题意:给出n个数组成的序列,求乘积最大的连续子序列 看的紫书,因为n最大为18,每个数最大为10,所以10^18用long long 能够存下, 直接枚举起点和终点找最大值就可以了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include&l

UVa11059 Maximum Product (Two Pointers)

链接:http://acm.hust.edu.cn/vjudge/problem/27946分析:Two Pointers搞搞. 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 typedef long long LL; 6 7 int n, a[20]; 8 9 LL gao() { 10 LL ans = 0; 11 for (int L = 0; L < n; L++) {

UVA 11059 Maximum Product(枚举start+end)

题目大意: 就是说给你n个数字,然后求出连续序列的最大和.如果ans为负数就输出0,要么就输出这个ans. 解题思路: 其实我们只需要枚举起点和终点,然后考虑所有的情况就可以了,有点类似于求解最大连续和的问题. 唯一的不同就是每次都要初始化t = 1, t*=a[j].注意long long.因为最多有18个数字,且每个数字的 最大值为10,那么他们的乘积是不会超过10^18的. 代码: # include<cstdio> # include<iostream> # include

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