LeetCode: Maximum Product Subarray && Maximum Subarray

Maximum Product Subarray

Title:

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.

对于Product Subarray,要考虑到一种特殊情况,即负数和负数相乘:如果前面得到一个较小的负数,和后面一个较大的负数相乘,得到的反而是一个较大的数,如{2,-3,-7},所以,我们在处理乘法的时候,除了需要维护一个局部最大值,同时还要维护一个局部最小值,由此,可以写出如下的转移方程式:

max_copy[i] = max_local[i]
max_local[i + 1] = Max(Max(max_local[i] * A[i], A[i]),  min_local * A[i])

min_local[i + 1] = Min(Min(max_copy[i] * A[i], A[i]),  min_local * A[i])

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int pmin = nums[0];
        int pmax = nums[0];
        int result = nums[0];
        for (int i = 1; i < nums.size(); i++){
            int tmax = pmax * nums[i];
            int tmin = pmin * nums[i];
            pmax = max(nums[i],max(tmax,tmin));
            pmin = min(nums[i],min(tmax,tmin));
            result = max(result,pmax);
        }
        return result;
    }
};

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.

class Solution{
public:
    int maxSubArray(int A[], int n) {
        int maxSum = A[0];
        int sum = A[0];
        for (int i = 1; i < n; i++){
            if (sum < 0)
                sum = 0;
            sum += A[i];
            maxSum = max(sum,maxSum);
        }
        return maxSum;
    }
};
时间: 2024-10-12 13:29:30

LeetCode: Maximum Product Subarray && Maximum Subarray的相关文章

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 求连续子数组使其乘积最大

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(动态规划)

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 OJ平台上Maximum Subarray题目O(n)复杂度解决方案

原始题目如下,意为寻找数组和最大的子串,返回这个最大和即可. 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.

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式

原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. 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 =

LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. LeetCode718. Maximum Length of Repeated Subarray 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连

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;