给定两个仅由正整数组成的等长数组A[]和B[] ,任务是反转第一个数组的任何子数组,使得两个数组的相同索引元素的乘积之和,即(A [i] * B[i])是最小值。
例子:
Input: N = 4, A[] = {2, 3, 1, 5}, B[] = {8, 2, 4, 3}
Output:
A[] = 1 3 2 5
B[] = 8 2 4 3
Minimum product is 37
Explanation: Reverse the subarray {A[0], A[2]}. Sum of product of same-indexed elements is 37, which is minimum possible.
Input: N = 3, A[] = {3, 1, 1}, B[] = {4, 5, 6}
Output:
A[] = 3 1 1
B[] = 4 5 6
Minimum product is 23
方法:给定的问题可以通过使用两个指针技术来解决。请按照以下步骤解决问题:
- 声明一个变量,比如total ,用于存储两个数组的初始乘积。
- 声明一个变量,例如min ,以存储所需的答案。
- 声明两个变量,比如first和second ,以存储需要反转以最小化乘积的子数组的开始和结束索引。
- 通过以下操作计算奇数长度子数组可能的最小乘积:
- 使用变量遍历数组,比如i 。
- 检查所有奇数长度子数组,通过设置i – 1 ,比如left和i + 1 ,比如right ,作为子数组的开始和结束索引。
- 通过减去a[left] * b[left] + a[right] * b[right]并添加 a[left] * b[right] + a[right] * b[left] 来更新总数。
- 对于每个子数组,在更新total 后,检查它是否是获得的最小值。相应地更新和存储最小产品。
- 同样,检查所有偶数长度子数组并计算总和。
- 最后,打印得到的数组和最小和。
下面是上述方法的实现:
C++
// C++ program to implement the above approach
#include
using namespace std;
// Function to print1 the arrays
void print1(vector &a, vector &b)
{
int minProd = 0;
for(int i = 0; i < a.size(); ++i)
{
cout << a[i] << " ";
}
cout << endl;
for(int i = 0; i < b.size(); ++i)
{
cout << b[i] << " ";
minProd += a[i] * b[i];
}
cout << endl;
cout << minProd;
}
// Function to reverse1 the subarray
void reverse1(int left, int right,
vector &arr)
{
while (left < right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
++left;
--right;
}
}
// Function to calculate the
// minimum product of same-indexed
// elements of two given arrays
void minimumProdArray(vector &a,
vector &b, int l)
{
int total = 0;
// Calculate initial product
for(int i = 0; i < a.size(); ++i)
{
total += a[i] * b[i];
}
int min = INT_MAX;
int first = 0;
int second = 0;
// Traverse all odd length subarrays
for(int i = 0; i < a.size(); ++i)
{
int left = i - 1;
int right = i + 1;
int total1 = total;
while (left >= 0 && right < a.size())
{
// Remove the previous product
total1 -= a[left] * b[left] +
a[right] * b[right];
// Add the current product
total1 += a[left] * b[right] +
a[right] * b[left];
// Check if current product
// is minimum or not
if (min >= total1)
{
min = total1;
first = left;
second = right;
}
--left;
++right;
}
}
// Traverse all even length subarrays
for(int i = 0; i < a.size(); ++i)
{
int left = i;
int right = i + 1;
int total1 = total;
while (left >= 0 && right < a.size())
{
// Remove the previous product
total1 -= a[left] * b[left] +
a[right] * b[right];
// Add to the current product
total1 += a[left] * b[right] +
a[right] * b[left];
// Check if current product
// is minimum or not
if (min >= total1)
{
min = total1;
first = left;
second = right;
}
// Update the pointers
--left;
++right;
}
}
// reverse1 the subarray
if (min < total)
{
reverse1(first, second, a);
// print1 the subarray
print1(a, b);
}
else
{
print1(a, b);
}
}
// Driver Code
int main()
{
int n = 4;
vector a{ 2, 3, 1, 5 };
vector b{ 8, 2, 4, 3 };
minimumProdArray(a, b, n);
}
// This code is contributed by bgangwar59
Java
// Java Program to implement the above approach
import java.io.*;
import java.util.*;
public class Main {
// Function to calculate the
// minimum product of same-indexed
// elements of two given arrays
static void minimumProdArray(long a[],
long b[], int l)
{
long total = 0;
// Calculate initial product
for (int i = 0; i < a.length; ++i) {
total += a[i] * b[i];
}
long min = Integer.MAX_VALUE;
int first = 0;
int second = 0;
// Traverse all odd length subarrays
for (int i = 0; i < a.length; ++i) {
int left = i - 1;
int right = i + 1;
long total1 = total;
while (left >= 0 && right < a.length) {
// Remove the previous product
total1 -= a[left] * b[left]
+ a[right] * b[right];
// Add the current product
total1 += a[left] * b[right]
+ a[right] * b[left];
// Check if current product
// is minimum or not
if (min >= total1) {
min = total1;
first = left;
second = right;
}
--left;
++right;
}
}
// Traverse all even length subarrays
for (int i = 0; i < a.length; ++i) {
int left = i;
int right = i + 1;
long total1 = total;
while (left >= 0 && right < a.length) {
// Remove the previous product
total1 -= a[left] * b[left]
+ a[right] * b[right];
// Add to the current product
total1 += a[left] * b[right]
+ a[right] * b[left];
// Check if current product
// is minimum or not
if (min >= total1) {
min = total1;
first = left;
second = right;
}
// Update the pointers
--left;
++right;
}
}
// Reverse the subarray
if (min < total) {
reverse(first, second, a);
// Print the subarray
print(a, b);
}
else {
print(a, b);
}
}
// Function to reverse the subarray
static void reverse(int left, int right,
long arr[])
{
while (left < right) {
long temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
++left;
--right;
}
}
// Function to print the arrays
static void print(long a[], long b[])
{
int minProd = 0;
for (int i = 0; i < a.length; ++i) {
System.out.print(a[i] + " ");
}
System.out.println();
for (int i = 0; i < b.length; ++i) {
System.out.print(b[i] + " ");
minProd += a[i] * b[i];
}
System.out.println();
System.out.println(minProd);
}
// Driver Code
public static void main(String[] args)
{
int n = 4;
long a[] = { 2, 3, 1, 5 };
long b[] = { 8, 2, 4, 3 };
minimumProdArray(a, b, n);
}
}
Python3
# Python3 Program to implement the above approach
# Function to calculate the
# minimum product of same-indexed
# elements of two given arrays
def minimumProdArray(a, b, l) :
total = 0
# Calculate initial product
for i in range(len(a)):
total += a[i] * b[i]
Min = 2147483647
first = 0;
second = 0;
# Traverse all odd length subarrays
for i in range(len(a)):
left = i - 1;
right = i + 1;
total1 = total;
while (left >= 0 and right < len(a)) :
# Remove the previous product
total1 -= a[left] * b[left] + a[right] * b[right];
# Add the current product
total1 += a[left] * b[right] + a[right] * b[left];
# Check if current product
# is minimum or not
if (Min >= total1) :
Min = total1
first = left
second = right
left -= 1
right += 1
# Traverse all even length subarrays
for i in range(len(a)):
left = i
right = i + 1
total1 = total
while (left >= 0 and right < len(a)) :
# Remove the previous product
total1 -= a[left] * b[left] + a[right] * b[right]
# Add to the current product
total1 += a[left] * b[right] + a[right] * b[left]
# Check if current product
# is minimum or not
if (Min >= total1) :
Min = total1
first = left
second = right
# Update the pointers
left -= 1
right += 1
# Reverse the subarray
if (Min < total) :
reverse(first, second, a)
# Print the subarray
Print(a, b)
else :
Print(a, b)
# Function to reverse the subarray
def reverse(left, right, arr) :
while (left < right) :
temp = arr[left]
arr[left] = arr[right]
arr[right] = temp
left += 1
right -= 1
# Function to print the arrays
def Print(a, b):
minProd = 0
for i in range(len(a)):
print(a[i], end = " ")
print();
for i in range(len(b)):
print(b[i], end = " ")
minProd += a[i] * b[i]
print()
print(minProd)
n = 4;
a = [ 2, 3, 1, 5 ]
b = [ 8, 2, 4, 3 ]
minimumProdArray(a, b, n)
# This code is contributed by divyeshrabadiya07
C#
// C# Program to implement the above approach
using System;
public class GFG {
// Function to calculate the
// minimum product of same-indexed
// elements of two given arrays
static void minimumProdArray(long[] a, long[] b, int l)
{
long total = 0;
// Calculate initial product
for (int i = 0; i < a.Length; ++i) {
total += a[i] * b[i];
}
long min = Int32.MaxValue;
int first = 0;
int second = 0;
// Traverse all odd length subarrays
for (int i = 0; i < a.Length; ++i) {
int left = i - 1;
int right = i + 1;
long total1 = total;
while (left >= 0 && right < a.Length) {
// Remove the previous product
total1 -= a[left] * b[left]
+ a[right] * b[right];
// Add the current product
total1 += a[left] * b[right]
+ a[right] * b[left];
// Check if current product
// is minimum or not
if (min >= total1) {
min = total1;
first = left;
second = right;
}
--left;
++right;
}
}
// Traverse all even length subarrays
for (int i = 0; i < a.Length; ++i) {
int left = i;
int right = i + 1;
long total1 = total;
while (left >= 0 && right < a.Length) {
// Remove the previous product
total1 -= a[left] * b[left]
+ a[right] * b[right];
// Add to the current product
total1 += a[left] * b[right]
+ a[right] * b[left];
// Check if current product
// is minimum or not
if (min >= total1) {
min = total1;
first = left;
second = right;
}
// Update the pointers
--left;
++right;
}
}
// Reverse the subarray
if (min < total) {
reverse(first, second, a);
// Print the subarray
print(a, b);
}
else {
print(a, b);
}
}
// Function to reverse the subarray
static void reverse(int left, int right, long[] arr)
{
while (left < right) {
long temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
++left;
--right;
}
}
// Function to print the arrays
static void print(long[] a, long[] b)
{
long minProd = 0;
for (int i = 0; i < a.Length; ++i) {
Console.Write(a[i] + " ");
}
Console.WriteLine();
for (int i = 0; i < b.Length; ++i) {
Console.Write(b[i] + " ");
minProd += a[i] * b[i];
}
Console.WriteLine();
Console.WriteLine(minProd);
}
// Driver Code
public static void Main(string[] args)
{
int n = 4;
long[] a = { 2, 3, 1, 5 };
long[] b = { 8, 2, 4, 3 };
minimumProdArray(a, b, n);
}
}
// This code is contributed by ukasp.
Javascript
输出:
1 3 2 5
8 2 4 3
37
时间复杂度: O(N 2 )。
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。