Leetcode 152. Maximum Product Subarray vector二维数组初始化赋值复习

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 6.

Example 2:

Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.

题目链接:https://leetcode.com/problems/maximum-product-subarray/

class Solution {
public:
    int maxProduct(vector& nums) {
       int n=nums.size();
        int m=nums[0];
        vector> dp(n,vector(2,0));
        dp[0][0]=m;
        dp[0][1]=m;//dp[i][0]最大值,dp[i][0]最小值
        
        for(int i=1;im)
                m=dp[i][0];
        }
        return m;
    }
};

其实那个dp二维数组,没有必要

class Solution {
public:
    int maxProduct(vector& nums) {
       int n=nums.size();
        int m=nums[0];
        int big=m;
        int small=m;
        for(int i=1;im)
                m=big;
        }
        return m;
    }
};

 

你可能感兴趣的:(Leetcode)