首页 > 代码库 > [leetcode]Maximum Product Subarray

[leetcode]Maximum Product Subarray

新题。。。不过很简单。。半年才更新一个题。。。

class Solution {public:    int maxProduct(int arr[], int n) {        int pmax = arr[0];        int pmin = arr[0];        int ans = arr[0];            for (int i = 1; i < n; i++) {            int tmax = arr[i] * pmax;            int tmin = arr[i] * pmin;            pmax = max(arr[i], max(tmax, tmin));            pmin = min(arr[i], min(tmax, tmin));            ans = max(pmax, ans);        }        return ans;    }};

 

[leetcode]Maximum Product Subarray