任意元素减 1 N 次后数组的最大乘积
给定一个大小为M的正整数数组arr[]和一个整数N ,任务是在从任何元素中减去1 N次后最大化数组的乘积
例子:
Input: M = 5, arr[] = {5, 1, 7, 8, 3}, N = 2
Output: 630
Explanation: After subtracting 1 from arr[3] 2 times, array becomes {5, 1, 7, 6, 3} with product = 630
Input: M = 2, arr[] = {2, 2}, N = 4
Output: 0
Explanation: After subtracting 2 from arr[0] and arr[1] 2 times each, array becomes {0, 0} with product = 0
方法:任务可以在 max-heap 的帮助下解决 按照以下步骤解决问题:
- 将所有元素插入最大堆内
- 从最大堆中弹出顶部元素,并从中减 1,同时减 N
- 将弹出的元素插入最大堆
- 继续这个过程直到 N > 0
- 最大乘积将是最大堆内所有元素的乘积
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum
// product of the array
int getMax(int m, int arr[], int n)
{
// Max-heap
priority_queue q;
// Store all the elements inside max-heap
for (int i = 0; i < m; i++)
q.push(arr[i]);
// n operations
while (n--) {
int x = q.top();
q.pop();
// Decrement x
--x;
// Push back x inside the heap
q.push(x);
}
// Store the max product possible
int ans = 1;
while (!q.empty()) {
ans *= q.top();
q.pop();
}
return ans;
}
// Driver Code
int main()
{
int M = 5;
int arr[5] = { 5, 1, 7, 8, 3 };
int N = 2;
cout << getMax(M, arr, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the maximum
// product of the array
static Integer getMax(int m, Integer arr[], int n)
{
// Max-heap
PriorityQueue q
= new PriorityQueue(
Collections.reverseOrder());
// Store all the elements inside max-heap
for (int i = 0; i < m; i++)
q.add(arr[i]);
// n operations
while (n-- != 0) {
Integer x = q.poll();
// Decrement x
--x;
// Push back x inside the heap
q.add(x);
}
// Store the max product possible
Integer ans = 1;
while (q.size() != 0) {
ans *= q.poll();
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int M = 5;
Integer arr[] = { 5, 1, 7, 8, 3 };
int N = 2;
System.out.println(getMax(M, arr, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# python program for the above approach
from queue import PriorityQueue
# Function to find the maximum
# product of the array
def getMax(m, arr, n):
# Max-heap
q = PriorityQueue()
# Store all the elements inside max-heap
for i in range(0, m):
q.put(-arr[i])
# n operations
while (n):
n -= 1
x = -q.get()
# Decrement x
x -= 1
# Push back x inside the heap
q.put(-x)
# Store the max product possible
ans = 1
while (not q.empty()):
ans *= -q.get()
return ans
# Driver Code
if __name__ == "__main__":
M = 5
arr = [5, 1, 7, 8, 3]
N = 2
print(getMax(M, arr, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to find the maximum
// product of the array
static int getMax(int m, int []arr, int n)
{
// Max-heap
List q
= new List();
// Store all the elements inside max-heap
for (int i = 0; i < m; i++){
q.Add(arr[i]);
}
q.Sort();
q.Reverse();
// n operations
while (n-- != 0) {
int x = q[0];
q.RemoveAt(0);
// Decrement x
--x;
// Push back x inside the heap
q.Add(x);
q.Sort();
q.Reverse();
}
// Store the max product possible
int ans = 1;
while (q.Count != 0) {
ans *= q[0];
q.RemoveAt(0);
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int M = 5;
int []arr = { 5, 1, 7, 8, 3 };
int N = 2;
Console.WriteLine(getMax(M, arr, N));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
630
时间复杂度:O(nlogm)
辅助空间:O(m)