大小为 K 的最小乘积子数组,包括负整数
给定一个长度为N的数组arr[] ,任务是找到一个包含负整数的数组的大小为K的子数组的最小乘积。
例子:
Input: arr = [2, 3, -1, -5, 4, 0], K = 3
Output: -6
Explanation: The product of the subarray {2, 3, -1} is -6 which is the minimum possible.
Input: arr = [-2, -4, 0, 1, 5, -6, 9], K =4
Output: -270
Explanation: The product of the subarray {1, 5, -6, 9} is -270 which is the minimum possible.
如果数组仅包含正数,则可以仅使用此处讨论的滑动窗口技术有效地解决问题。
方法:给定的问题可以使用两指针技术和滑动窗口方法来解决。可以按照以下步骤解决问题:
- 迭代数组arr直到K – 1并乘以每个非零数字以找到乘积,并计算子数组中零的数量。
- 从K迭代数组arr直到数组的末尾,并且在每次迭代时:
- 如果当前元素 不是零,然后将其乘以乘积,否则增加零的计数
- 如果当前滑动窗口左边的元素 不是零,然后将乘积除以该元素,否则减少零的计数
- 如果滑动窗口中的零个数为 0 然后用当前产品更新结果,或者用零更新它
- 返回结果res 。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to find the minimum
// product of subarray of size K
int minProdK(vector& arr, int K)
{
// Initialize prod to 1
// and zeros to 0
int prod = 1, zeros = 0;
// Initialize length of the array
int N = arr.size();
// Iterate the array arr till K - 1
for (int i = 0; i < K; i++) {
// If current element is 0
// then increment zeros
if (arr[i] == 0)
zeros++;
// Else multiply current
// element to prod
else
prod *= arr[i];
}
// Initialize prod to 0 if zeros > 0
// else to prod
int res = zeros == 0 ? prod : 0;
// Iterate the array arr
// from K till the end
for (int right = K; right < N; right++) {
// If current element is 0
// then increment zeros
if (arr[right] == 0)
zeros++;
// Else multiply arr[right]
// to prod
else
prod *= arr[right];
// If leftmost element in
// the sliding window is 0
// then decrement zeros
if (arr[right - K] == 0)
zeros--;
// Else divide prod by arr[right-K]
else
prod /= arr[right - K];
// If zeros == 0 then update
// res by taking minimum value
// of res and prod
if (zeros == 0)
res = min(res, prod);
// If zeros > 0 and res > 0
// then initialize res to 0
else if (res > 0)
res = 0;
}
// Return the answer as res
return res;
}
// Driver code
int main()
{
// Initialize the array
vector arr = { 2, 3, -1, -5, 4, 0 };
// Initialize the value of K
int K = 3;
// Call the function and
// print the result
cout << minProdK(arr, K);
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java implementation for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the minimum
// product of subarray of size K
public static int minProdK(
int arr[], int K)
{
// Initialize prod to 1
// and zeros to 0
int prod = 1, zeros = 0;
// Initialize length of the array
int N = arr.length;
// Iterate the array arr till K - 1
for (int i = 0; i < K; i++) {
// If current element is 0
// then increment zeros
if (arr[i] == 0)
zeros++;
// Else multiply current
// element to prod
else
prod *= arr[i];
}
// Initialize prod to 0 if zeros > 0
// else to prod
int res = zeros == 0 ? prod : 0;
// Iterate the array arr
// from K till the end
for (int right = K; right < N; right++) {
// If current element is 0
// then increment zeros
if (arr[right] == 0)
zeros++;
// Else multiply arr[right]
// to prod
else
prod *= arr[right];
// If leftmost element in
// the sliding window is 0
// then decrement zeros
if (arr[right - K] == 0)
zeros--;
// Else divide prod by arr[right-K]
else
prod /= arr[right - K];
// If zeros == 0 then update
// res by taking minimum value
// of res and prod
if (zeros == 0)
res = Math.min(res, prod);
// If zeros > 0 and res > 0
// then initialize res to 0
else if (res > 0)
res = 0;
}
// Return the answer as res
return res;
}
// Driver code
public static void main(String[] args)
{
// Initialize the array
int[] arr = { 2, 3, -1, -5, 4, 0 };
// Initialize the value of K
int K = 3;
// Call the function and
// print the result
System.out.println(minProdK(arr, K));
}
}
Python3
# Python 3 implementation for the above approach
# Function to find the minimum
# product of subarray of size K
def minProdK(arr, K):
# Initialize prod to 1
# and zeros to 0
prod = 1
zeros = 0
# Initialize length of the array
N = len(arr)
# Iterate the array arr till K - 1
for i in range(K):
# If current element is 0
# then increment zeros
if (arr[i] == 0):
zeros += 1
# Else multiply current
# element to prod
else:
prod *= arr[i]
# Initialize prod to 0 if zeros > 0
# else to prod
if zeros == 0:
res = prod
else:
res = 0
# Iterate the array arr
# from K till the end
for right in range(K, N):
# If current element is 0
# then increment zeros
if (arr[right] == 0):
zeros += 1
# Else multiply arr[right]
# to prod
else:
prod *= arr[right]
# If leftmost element in
# the sliding window is 0
# then decrement zeros
if (arr[right - K] == 0):
zeros -= 1
# Else divide prod by arr[right-K]
else:
prod //= arr[right - K]
# If zeros == 0 then update
# res by taking minimum value
# of res and prod
if (zeros == 0):
res = min(res, prod)
# If zeros > 0 and res > 0
# then initialize res to 0
elif (res > 0):
res = 0
# Return the answer as res
return res
# Driver code
if __name__ == "__main__":
# Initialize the array
arr = [2, 3, -1, -5, 4, 0]
# Initialize the value of K
K = 3
# Call the function and
# print the result
print(minProdK(arr, K))
# This code is contributed by ukasp.
C#
// C# implementation for the above approach
using System;
class GFG
{
// Function to find the minimum
// product of subarray of size K
public static int minProdK(
int []arr, int K)
{
// Initialize prod to 1
// and zeros to 0
int prod = 1, zeros = 0;
// Initialize length of the array
int N = arr.Length;
// Iterate the array arr till K - 1
for (int i = 0; i < K; i++) {
// If current element is 0
// then increment zeros
if (arr[i] == 0)
zeros++;
// Else multiply current
// element to prod
else
prod *= arr[i];
}
// Initialize prod to 0 if zeros > 0
// else to prod
int res = zeros == 0 ? prod : 0;
// Iterate the array arr
// from K till the end
for (int right = K; right < N; right++) {
// If current element is 0
// then increment zeros
if (arr[right] == 0)
zeros++;
// Else multiply arr[right]
// to prod
else
prod *= arr[right];
// If leftmost element in
// the sliding window is 0
// then decrement zeros
if (arr[right - K] == 0)
zeros--;
// Else divide prod by arr[right-K]
else
prod /= arr[right - K];
// If zeros == 0 then update
// res by taking minimum value
// of res and prod
if (zeros == 0)
res = Math.Min(res, prod);
// If zeros > 0 and res > 0
// then initialize res to 0
else if (res > 0)
res = 0;
}
// Return the answer as res
return res;
}
// Driver code
public static void Main()
{
// Initialize the array
int[] arr = { 2, 3, -1, -5, 4, 0 };
// Initialize the value of K
int K = 3;
// Call the function and
// print the result
Console.Write(minProdK(arr, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
-6
时间复杂度: O(N)
辅助空间: O(1)