给定两个大小为 n 的数组 A 和 B,任务是找到 A[0] * B[0] + A[1] * B[1] +…+ A[n-1] 的最小值* B[n-1]。允许对数组 A 和 B 的元素进行混洗。
例子 :
Input : A[] = {3, 1, 1} and B[] = {6, 5, 4}.
Output : 23
Minimum value of S = 1*6 + 1*5 + 3*4 = 23.
Input : A[] = { 6, 1, 9, 5, 4 } and B[] = { 3, 4, 8, 2, 4 }
Output : 80.
Minimum value of S = 1*8 + 4*4 + 5*4 + 6*3 + 9*2 = 80.
这个想法是将一个数组的最小元素乘以另一个数组的最大元素。解决这个问题的算法:
- 对数组 A 和 B 进行排序。
- 遍历数组,对于每个元素,乘以 A[i] 和 B[n – i – 1] 并添加到总数中。
注意:我们正在添加可能导致溢出条件的元素的乘法。
下图是上述方法的说明:
下面是上述方法的实现:
C++
// C++ program to calculate minimum sum of product
// of two arrays.
#include
using namespace std;
// Returns minimum sum of product of two arrays
// with permutations allowed
long long int minValue(int A[], int B[], int n)
{
// Sort A and B so that minimum and maximum
// value can easily be fetched.
sort(A, A + n);
sort(B, B + n);
// Multiplying minimum value of A and maximum
// value of B
long long int result = 0;
for (int i = 0; i < n; i++)
result += (A[i] * B[n - i - 1]);
return result;
}
// Driven Code
int main()
{
int A[] = { 3, 1, 1 };
int B[] = { 6, 5, 4 };
int n = sizeof(A) / sizeof(A[0]);
cout << minValue(A, B, n) << endl;
return 0;
}
Java
// Java program to calculate minimum
// sum of product of two arrays.
import java.io.*;
import java.util.*;
class GFG {
// Returns minimum sum of product of two arrays
// with permutations allowed
static long minValue(int A[], int B[], int n)
{
// Sort A and B so that minimum and maximum
// value can easily be fetched.
Arrays.sort(A);
Arrays.sort(B);
// Multiplying minimum value of A
// and maximum value of B
long result = 0;
for (int i = 0; i < n; i++)
result += (A[i] * B[n - i - 1]);
return result;
}
// Driven Code
public static void main(String[] args)
{
int A[] = { 3, 1, 1 };
int B[] = { 6, 5, 4 };
int n = A.length;
;
System.out.println(minValue(A, B, n));
}
}
// This code is contributed by vt_m
Python
# Python program to calculate minimum sum of product
# of two arrays.
# Returns minimum sum of product of two arrays
# with permutations allowed
def minValue(A, B, n):
# Sort A and B so that minimum and maximum
# value can easily be fetched.
A.sort()
B.sort()
# Multiplying minimum value of A and maximum
# value of B
result = 0
for i in range(n):
result += (A[i] * B[n - i - 1])
return result
# Driven Program
A = [3, 1, 1]
B = [6, 5, 4]
n = len(A)
print minValue(A, B, n)
# Contributed by: Afzal Ansari
C#
// C# program to calculate minimum
// sum of product of two arrays.
using System;
class GFG {
// Returns minimum sum of product
// of two arrays with permutations
// allowed
static long minValue(int[] a, int[] b,
int n)
{
// Sort A and B so that minimum
// and maximum value can easily
// be fetched.
Array.Sort(a);
Array.Sort(b);
// Multiplying minimum value of
// A and maximum value of B
long result = 0;
for (int i = 0; i < n; i++)
result += (a[i] * b[n - i - 1]);
return result;
}
// Driven Code
public static void Main()
{
int[] a = { 3, 1, 1 };
int[] b = { 6, 5, 4 };
int n = a.Length;
Console.Write(minValue(a, b, n));
}
}
// This code is contributed by nitin mittal.
PHP
Javascript
输出 :
23
时间复杂度: O(n log n)。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。