给定两个阵列A []和B []由N个整数的,则任务是更新数组A []通过向一个单一的元素B [j]与更新A [1]到A [分配每个数组元素A [i]于i] + B[j]或A[i] * B[j] ,使得数组A[]的乘积最大化。
注意:两个数组中的每个数组元素只能与另一个数组中的单个元素配对一次。
例子:
Input: A[] = {1, 1, 6}, B[] = {1, 2, 3}
Output: 108
Explanation:
- Update A[0] = A[0] + B[0], A[] modifies to {2, 1, 6}
- Update A[1] = A[1] + B[1], A[] modifies to {2, 3, 6}
- Update A[0] = A[0] * B[2], A[] modifies to {6, 3, 6}
Therefore, the product of the array A[] is 6 * 3 * 6 = 108.
Input: A[] = {1, 1, 10}, B[] ={1, 1, 1}
Output: 60
Explanation:
- Update A[0] = A[0] + B[0], A[] modifies to {2, 1, 10}
- Update A[1] = A[1] + B[1], A[] modifies to {2, 2, 10}
- Update A[0] = A[0] * B[2], A[] modifies to {3, 2, 10}
方法:上述问题可以通过使用优先级队列(min-heap)来解决。请按照以下步骤解决问题:
- 对数组B[] 进行排序。
- 将数组A[] 的所有元素插入优先级队列,以便每次获得最少的元素。
- 使用变量j遍历给定的数组B[]并从优先级队列中弹出一个元素作为minE + B[j]或minE*B[j] 的最大值并将这个最大值推入优先级队列。
- 经过以上步骤,优先级队列中元素的乘积就是需要的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the largest
// product of array A[]
int largeProduct(vector A,
vector B, int N)
{
// Base Case
if (N == 0)
return 0;
// Store all the elements of
// the array A[]
priority_queue,
greater> pq;
for(int i = 0; i < N; i++)
pq.push(A[i]);
// Sort the Array B[]
sort(B.begin(), B.end());
// Traverse the array B[]
for(int i = 0; i < N; i++)
{
// Pop minimum element
int minn = pq.top();
pq.pop();
// Check which operation is
// producing maximum element
int maximized_element = max(minn * B[i],
minn + B[i]);
// Insert resultant element
// into the priority queue
pq.push(maximized_element);
}
// Evaluate the product
// of the elements of A[]
int max_product = 1;
while (pq.size() > 0)
{
max_product *= pq.top();
pq.pop();
}
// Return the maximum product
return max_product;
}
// Driver Code
int main()
{
// Given arrays
vector A = { 1, 1, 10 };
vector B = { 1, 1, 1 };
int N = 3;
// Function Call
cout << largeProduct(A, B, N);
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the largest
// product of array A[]
public static int largeProduct(
int A[], int B[], int N)
{
// Base Case
if (N == 0)
return 0;
// Store all the elements of
// the array A[]
PriorityQueue pq
= new PriorityQueue<>();
for (int i = 0; i < N; i++)
pq.add(A[i]);
// Sort the Array B[]
Arrays.sort(B);
// Traverse the array B[]
for (int i = 0; i < N; i++) {
// Pop minimum element
int minn = pq.poll();
// Check which operation is
// producing maximum element
int maximized_element
= Math.max(minn * B[i],
minn + B[i]);
// Insert resultant element
// into the priority queue
pq.add(maximized_element);
}
// Evaluate the product
// of the elements of A[]
int max_product = 1;
while (pq.size() > 0) {
max_product *= pq.poll();
}
// Return the maximum product
return max_product;
}
// Driver Code
public static void main(String[] args)
{
// Given arrays
int A[] = { 1, 1, 10 };
int B[] = { 1, 1, 1 };
int N = 3;
// Function Call
System.out.println(
largeProduct(A, B, N));
}
}
Python3
# Python program for the above approach
# Function to find the largest
# product of array A[]
def largeProduct(A, B, N):
# Base Case
if(N == 0):
return 0
# Store all the elements of
# the array A[]
pq = []
for i in range(N):
pq.append(A[i])
# Sort the Array B[]
B.sort()
pq.sort(reverse = True)
# Traverse the array B[]
for i in range(N):
# Pop minimum element
minn = pq.pop()
# Check which operation is
# producing maximum element
maximized_element = max(minn * B[i], minn + B[i])
# Insert resultant element
# into the priority queue
pq.append(maximized_element)
pq.sort(reverse = True)
# Evaluate the product
# of the elements of A[]
max_product = 1
while(len(pq) > 0):
max_product *= pq.pop();
# Return the maximum product
return max_product
# Driver Code
# Given arrays
A = [1, 1, 10]
B = [1, 1, 1]
N = 3
# Function Call
print(largeProduct(A, B, N))
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the largest
// product of array A[]
public static int largeProduct(int[] A, int[] B, int N)
{
// Base Case
if(N == 0)
{
return 0;
}
// Store all the elements of
// the array A[]
List pq = new List();
for(int i = 0; i < N; i++)
{
pq.Add(A[i]);
}
// Sort the Array B[]
Array.Sort(B);
pq.Sort();
// Traverse the array B[]
for(int i = 0; i < N; i++)
{
int min = pq[0];
// Pop minimum element
pq.RemoveAt(0);
// Check which operation is
// producing maximum element
int maximized_element = Math.Max(min* B[i], min + B[i]);
// Insert resultant element
// into the priority queue
pq.Add(maximized_element);
pq.Sort();
}
// Evaluate the product
// of the elements of A[]
int max_product = 1;
while(pq.Count > 0)
{
max_product *= pq[0];
pq.RemoveAt(0);
}
// Return the maximum product
return max_product;
}
// Driver Code
static public void Main ()
{
// Given arrays
int[] A = { 1, 1, 10 };
int[] B = { 1, 1, 1 };
int N = 3;
// Function Call
Console.WriteLine(largeProduct(A, B, N));
}
}
// This code is contributed by rag2127
输出:
60
时间复杂度: O(N log N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live