给定一个同时包含正整数和负整数的数组,请找到最大乘积子数组的乘积。预期的时间复杂度为O(n),并且只能使用O(1)的额外空间。最大积可以为正,负或零。
例子:
Input : arr[] = {-2, -3, 0, -2, -40}
Output : 80
Subarray : arr[3..4] i.e.{-2, -40}
Input : arr[] = {0, -4, 0, -2}
Output : 0
推荐:请首先在IDE上尝试您的方法,然后查看解决方案。
我们已经在“最大乘积子数组”中讨论了此问题,但是存在一个限制,即结果只能是肯定的。为了使最大乘积为负或零,必须按以下方式更新变量maxval(直到当前元素的最大乘积)和minval(直到当前元素的最小乘积)的值:
- 当arr [i]为正时:由于maxval是最大可能值,只需将arr [i]与maxval相乘即可获得新的maxval。 minval是最小可能的负积。如果其先前的值为负,则只需将其与arr [i]相乘即可。如果其值为1,则将其保留为1。
- 当arr [i]为0时:考虑测试用例:arr [] = {0,-4,0,-2}。在这种情况下,最大积为0。为了在我们的算法中解决这种情况,每当arr [i]为零时,将maxval更新为0而不是1。任何数字与零的乘积为零。考虑另一个测试用例,arr [] = {0,1,2}。
如果在当前迭代之后(根据上述步骤)maxval保持为零,并且下一个元素为正,则结果将为零,而不是正元素。要考虑在每次迭代结束时检查maxval是否为零。
如果将其设置为零,则将其等于1。用1作为子数组乘积(其中元素为零)更新minval将为零,这会导致损失最小可能值。因此,通过将minval设置为1,从子数组中排除此零,即重新开始乘积计算。 - 当arr [i]为负时: maxval的新值为先前的minval * arr [i],minval的新值为先前的maxval * arr [i]。在更新maxval之前,请将其先前的值存储在prevMax中以用于更新minval。
执行:
C++
// C++ program to find maximum subarray product.
#include
using namespace std;
// Function to find maximum subarray product.
int findMaxProduct(int arr[], int n)
{
int i;
// As maximum product can be negative, so
// initialize ans with minimum integer value.
int ans = INT_MIN;
// Variable to store maximum product until
// current value.
int maxval = 1;
// Variable to store minimum product until
// current value.
int minval = 1;
// Variable used during updation of maximum
// product and minimum product.
int prevMax;
for (i = 0; i < n; i++) {
// If current element is positive, update
// maxval. Update minval if it is
// negative.
if (arr[i] > 0) {
maxval = maxval * arr[i];
minval = min(1, minval * arr[i]);
}
// If current element is zero, maximum
// product cannot end at current element.
// Update minval with 1 and maxval with 0.
// maxval is updated to 0 as in case all
// other elements are negative, then maxval
// is 0.
else if (arr[i] == 0) {
minval = 1;
maxval = 0;
}
// If current element is negative, then new
// value of maxval is previous minval*arr[i]
// and new value of minval is previous
// maxval*arr[i]. Before updating maxval,
// store its previous value in prevMax to
// be used to update minval.
else if (arr[i] < 0) {
prevMax = maxval;
maxval = minval * arr[i];
minval = prevMax * arr[i];
}
// Update ans if necessary.
ans = max(ans, maxval);
// If maxval is zero, then to calculate
// product for next iteration, it should
// be set to 1 as maximum product
// subarray does not include 0.
// The minimum possible value
// to be considered in maximum product
// subarray is already stored in minval,
// so when maxval is negative it is set to 1.
if (maxval <= 0) {
maxval = 1;
}
}
return ans;
}
int main()
{
int arr[] = { 0, -4, 0, -2 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << findMaxProduct(arr, n);
return 0;
}
Java
// Java program to find maximum subarray product.
class GFG{
// Function to find maximum subarray product.
static int findMaxProduct(int arr[], int n)
{
int i;
// As maximum product can be negative, so
// initialize ans with minimum integer value.
int ans = Integer.MIN_VALUE;
// Variable to store maximum product until
// current value.
int maxval = 1;
// Variable to store minimum product until
// current value.
int minval = 1;
// Variable used during updation of maximum
// product and minimum product.
int prevMax;
for (i = 0; i < n; i++) {
// If current element is positive, update
// maxval. Update minval if it is
// negative.
if (arr[i] > 0) {
maxval = maxval * arr[i];
minval = Math.min(1, minval * arr[i]);
}
// If current element is zero, maximum
// product cannot end at current element.
// Update minval with 1 and maxval with 0.
// maxval is updated to 0 as in case all
// other elements are negative, then maxval
// is 0.
else if (arr[i] == 0) {
minval = 1;
maxval = 0;
}
// If current element is negative, then new
// value of maxval is previous minval*arr[i]
// and new value of minval is previous
// maxval*arr[i]. Before updating maxval,
// store its previous value in prevMax to
// be used to update minval.
else if (arr[i] < 0) {
prevMax = maxval;
maxval = minval * arr[i];
minval = prevMax * arr[i];
}
// Update ans if necessary.
ans = Math.max(ans, maxval);
// If maxval is zero, then to calculate
// product for next iteration, it should
// be set to 1 as maximum product
// subarray does not include 0.
// The minimum possible value
// to be considered in maximum product
// subarray is already stored in minval,
// so when maxval is negative it is set to 1.
if (maxval <= 0) {
maxval = 1;
}
}
return ans;
}
public static void main(String[] args) {
int arr[] = { 0, -4, 0, -2 };
int n = arr.length;
System.out.println(findMaxProduct(arr, n));
}
}
Python3
# Python3 program to find maximum
# subarray product.
# Function to find maximum
# subarray product.
def findMaxProduct(arr, n):
# As maximum product can be negative,
# so initialize ans with minimum
# integer value.
ans = -float('inf')
# Variable to store maximum product
# until current value.
maxval = 1
# Variable to store minimum product
# until current value.
minval = 1
for i in range(0, n):
# If current element is positive,
# update maxval. Update minval
# if it is negative.
if arr[i] > 0:
maxval = maxval * arr[i]
minval = min(1, minval * arr[i])
# If current element is zero, maximum
# product cannot end at current element.
# Update minval with 1 and maxval with 0.
# maxval is updated to 0 as in case all
# other elements are negative, then
# maxval is 0.
elif arr[i] == 0:
minval = 1
maxval = 0
# If current element is negative,
# then new value of maxval is previous
# minval*arr[i] and new value of minval
# is previous maxval*arr[i]. Before
# updating maxval, store its previous
# value in prevMax to be used to
# update minval.
elif arr[i] < 0:
prevMax = maxval
maxval = minval * arr[i]
minval = prevMax * arr[i]
# Update ans if necessary.
ans = max(ans, maxval)
# If maxval is zero, then to calculate
# product for next iteration, it should
# be set to 1 as maximum product subarray
# does not include 0. The minimum possible
# value to be considered in maximum product
# subarray is already stored in minval,
# so when maxval is negative it is set to 1.
if maxval <= 0:
maxval = 1
return ans
# Driver Code
if __name__ == "__main__":
arr = [ 0, -4, 0, -2 ]
n = len(arr)
print(findMaxProduct(arr, n))
# This code is contributed
# by Rituraj Jain
C#
// C# program to find maximum subarray product.
using System;
class GFG
{
// Function to find maximum subarray product.
static int findMaxProduct(int[] arr, int n)
{
int i;
// As maximum product can be negative, so
// initialize ans with minimum integer value.
int ans = Int32.MinValue;
// Variable to store maximum product until
// current value.
int maxval = 1;
// Variable to store minimum product until
// current value.
int minval = 1;
// Variable used during updation of maximum
// product and minimum product.
int prevMax;
for (i = 0; i < n; i++)
{
// If current element is positive, update
// maxval. Update minval if it is
// negative.
if (arr[i] > 0)
{
maxval = maxval * arr[i];
minval = Math.Min(1, minval * arr[i]);
}
// If current element is zero, maximum
// product cannot end at current element.
// Update minval with 1 and maxval with 0.
// maxval is updated to 0 as in case all
// other elements are negative, then maxval
// is 0.
else if (arr[i] == 0)
{
minval = 1;
maxval = 0;
}
// If current element is negative, then new
// value of maxval is previous minval*arr[i]
// and new value of minval is previous
// maxval*arr[i]. Before updating maxval,
// store its previous value in prevMax to
// be used to update minval.
else if (arr[i] < 0)
{
prevMax = maxval;
maxval = minval * arr[i];
minval = prevMax * arr[i];
}
// Update ans if necessary.
ans = Math.Max(ans, maxval);
// If maxval is zero, then to calculate
// product for next iteration, it should
// be set to 1 as maximum product
// subarray does not include 0.
// The minimum possible value
// to be considered in maximum product
// subarray is already stored in minval,
// so when maxval is negative it is set to 1.
if (maxval <= 0)
{
maxval = 1;
}
}
return ans;
}
// Driver Code
public static void Main()
{
int[] arr = { 0, -4, 0, -2 };
int n = arr.Length;
Console.WriteLine(findMaxProduct(arr, n));
}
}
// This code is contributed
// by Akanksha Rai
PHP
0)
{
$maxval = $maxval * $arr[i];
$minval = min(1, $minval * $arr[$i]);
}
// If current element is zero, maximum
// product cannot end at current element.
// Update minval with 1 and maxval with 0.
// maxval is updated to 0 as in case all
// other elements are negative, then maxval
// is 0.
else if ($arr[$i] == 0)
{
$minval = 1;
$maxval = 0;
}
// If current element is negative, then new
// value of maxval is previous minval*arr[i]
// and new value of minval is previous
// maxval*arr[i]. Before updating maxval,
// store its previous value in prevMax to
// be used to update minval.
else if ($arr[$i] < 0)
{
$prevMax = $maxval;
$maxval = $minval * $arr[$i];
$minval = $prevMax * $arr[$i];
}
// Update ans if necessary.
$ans = max($ans, $maxval);
// If maxval is zero, then to calculate
// product for next iteration, it should
// be set to 1 as maximum product
// subarray does not include 0.
// The minimum possible value
// to be considered in maximum product
// subarray is already stored in minval,
// so when maxval is negative it is set to 1.
if ($maxval <= 0)
{
$maxval = 1;
}
}
return $ans;
}
// Driver Code
$arr = array( 0, -4, 0, -2 );
$n = sizeof($arr);
echo findMaxProduct($arr, $n);
// This code is contributed by ita_c
?>
Javascript
输出:
0
时间复杂度: O(n)
辅助空间: O(1)