日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【LeetCode】152. Maximum Product Subarray

發布時間:2024/7/19 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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.

題解:

  先暴力解,遍歷所有組合,更新最大值。很顯然得超時。

Solution 1 (TLE)

class Solution { public:int maxProduct(vector<int>& nums) {int n = nums.size(), mproduct = nums[0];for (int i = 0; i < n; ++i) {int tmp = nums[i];mproduct = max(mproduct, tmp);for (int j = i + 1; j < n; ++j) {tmp = tmp * nums[j];mproduct = max(mproduct, tmp);}}return mproduct;} };

  Besides keeping track of the largest product, we also need to keep track of the smallest product. Why? The smallest product, which is the largest in the negative sense could become the maximum when being multiplied by a negative number. (from here)

  Let us denote that:

f(k) = Largest product subarray, from index 0 up to k.

?  Similarly,

g(k) = Smallest product subarray, from index 0 up to k.

?  Then,

f(k) = max( f(k-1) * A[k], A[k], g(k-1) * A[k] ) g(k) = min( g(k-1) * A[k], A[k], f(k-1) * A[k] )

Solution 2 ()

class Solution { public:int maxProduct(vector<int>& nums) {int maxPro = nums[0], minPro = nums[0], result = nums[0], n = nums.size();for (int i=1; i<n; i++) {int mx = maxPro, mn = minPro;maxPro = max(max(nums[i], mx * nums[i]), mn * nums[i]);minPro = min(min(nums[i], mx * nums[i]), mn * nums[i]);result = max(maxPro, result);}return result;} };
  • Fist we assume there is no zero in the A[]. The answer must be A[0]?A[1]?.... A[i] OR A[j] *A[j+1] A[n - 1]. (Try to prove yourself)

  • Then when we have zero in the A[] (assum A[k] == 0). We could see A[0],A[1]...A[k - 1 ] As An Array and A[k + 1] A[k + 2]...A[n-1] is another.(from here)

  •   The key point of this problem is: there are only two patterns:
    One is "aBcD", and the other is "aBcDe", where I use lowercase to denote a negative number, and use upper case to denote a positive number.
    For the first pattern, the maximum product would be "aBcD"; and for the second pattern, the maximum product would be "max (aBcD, BcDe)". So above solution code is very elegant and efficient.?

    Solution 3 ()

    class Solution {// author : s2003zy// weibo : http://weibo.com/574433433// blog : http://s2003zy.com// Time : O(n)// Space : O(1)public:int maxProduct(vector<int>& nums) {int ans = INT_MIN, frontProduct = 1, backProduct = 1;int n = nums.size();for(int i = 0; i < n; ++i) {frontProduct *= nums[i];backProduct *= nums[n - i - 1];ans = max(ans,max(frontProduct,backProduct));frontProduct = frontProduct == 0 ? 1 : frontProduct;backProduct = backProduct == 0 ? 1 : backProduct;}return ans;} };

    ?

    轉載于:https://www.cnblogs.com/Atanisi/p/6729865.html

    總結

    以上是生活随笔為你收集整理的【LeetCode】152. Maximum Product Subarray的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。