Maximum Product Subarray Problem & Solution

See the maximum product subarray problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")

static const int _=[](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
public:
  int maxProduct(vector<int>& nums) {
    int low = nums[0], high = nums[0], result = nums[0];
    for (int i = 1; i < nums.size(); ++i) {
      if (nums[i] < 0) {
        swap(low, high);
      }

      low = min(nums[i], low * nums[i]);
      high = max(nums[i], high * nums[i]);

      result = max(result, high);
    }

    return result;
  }
};

Start Here

Many Paths. Follow Yours.