给定一个由N 个正整数组成的数组arr[] ,其中arr[i]表示第i个供应商拥有的产品数量和一个正整数M ,任务是通过销售M产品找到最大利润,如果利润特定产品的数量与该供应商剩余的产品数量相同。
例子:
Input: arr[] = {4, 6}, M = 4
Output: 19
Explanation:
Below are the order of the product sell to gain the maximum profit:
Product 1: Sell a product from the second supplier, then the array modifies to {4, 5} and the profit is 6.
Product 2: Sell a product from the second supplier, then the array modifies to{4, 4} and the profit is 6 + 5 = 11.
Product 3: Sell a product from the second supplier, then the array modifies to {4, 3} and the profit is 6 + 5 + 4 = 15.
Product 4: Sell a product from the first supplier, then the array modifies to {3, 3} and the profit is 6 + 5 + 4 + 4 = 19.
Therefore, the maximum profit that can be obtained by selling 4 products is 19.
Input: arr[] = {1, 2, 3}, M = 2
Output: 5
朴素方法:可以通过从当前剩余产品数量最多的供应商处销售产品来解决给定的问题。所以,思路是迭代一个循环M次,在每次迭代中找到数组中最大元素的值,并将其值添加到利润中,然后将其在数组中的值减1。 循环后,打印利润的价值。
时间复杂度: O(M * N)
辅助空间: O(1)
高效的方法:上述方法也可以通过使用最大堆来优化,以在O(log N)时间内跟踪数组中的最大元素。请按照以下步骤解决问题:
- 使用优先级队列初始化最大堆,比如Q以跟踪数组中存在的最大元素。
- 遍历数组arr[]并插入堆Q 中的所有元素。
- 初始化一个变量,比如maxProfit为0来存储获得的结果最大利润。
- 迭代一个循环直到M > 0 ,并执行以下步骤:
- 将M的值减少1 。
- 将优先级队列Q的顶部元素的值存储在变量X 中,并将其从优先级队列中弹出。
- 将X的值添加到变量maxProfit并将(X – 1)插入变量Q 。
- 完成上述步骤后,打印maxProfit的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum profit
// by selling M number of products
void findMaximumProfit(int arr[], int M, int N)
{
// Initialize a Max-Heap to keep
// track of the maximum value
priority_queue max_heap;
// Stores the maximum profit
int maxProfit = 0;
// Traverse the array and push
// all the elements in max_heap
for(int i = 0; i < N; i++)
max_heap.push(arr[i]);
// Iterate a loop until M > 0
while (M > 0)
{
// Decrement the value
// of M by 1
M--;
// Pop the maximum element
// from the heap
int X = max_heap.top();
max_heap.pop();
// Update the maxProfit
maxProfit += X;
// Push (X - 1) to max heap
max_heap.push(X - 1);
}
// Print the result
cout<
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the maximum profit
// by selling M number of products
static void findMaximumProfit(
int[] arr, int M, int N)
{
// Initialize a Max-Heap to keep
// track of the maximum value
PriorityQueue max_heap
= new PriorityQueue<>((a, b) -> b - a);
// Stores the maximum profit
int maxProfit = 0;
// Traverse the array and push
// all the elements in max_heap
for (int i = 0; i < N; i++)
max_heap.add(arr[i]);
// Iterate a loop until M > 0
while (M > 0) {
// Decrement the value
// of M by 1
M--;
// Pop the maximum element
// from the heap
int X = max_heap.poll();
// Update the maxProfit
maxProfit += X;
// Push (X - 1) to max heap
max_heap.add(X - 1);
}
// Print the result
System.out.println(maxProfit);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 4, 6 };
int M = 4;
int N = arr.length;
findMaximumProfit(arr, M, N);
}
}
Python3
# Python3 program for the above approach
# Function to find the maximum profit
# by selling M number of products
def findMaximumProfit(arr, M, N):
# Initialize a Max-Heap to keep
# track of the maximum value
max_heap = []
# Stores the maximum profit
maxProfit = 0
# Traverse the array and push
# all the elements in max_heap
for i in range(0, N):
max_heap.append(arr[i])
max_heap.sort()
max_heap.reverse()
# Iterate a loop until M > 0
while (M > 0):
# Decrement the value
# of M by 1
M -= 1
# Pop the maximum element
# from the heap
X = max_heap[0]
max_heap.pop(0)
# Update the maxProfit
maxProfit += X
# Push (X - 1) to max heap
max_heap.append(X - 1)
max_heap.sort()
max_heap.reverse()
# Print the result
print(maxProfit)
# Driver code
arr = [ 4, 6 ]
M = 4
N = len(arr)
findMaximumProfit(arr, M, N)
# This code is contributed by rameshtravel07
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find the maximum profit
// by selling M number of products
static void findMaximumProfit(int[] arr, int M, int N)
{
// Initialize a Max-Heap to keep
// track of the maximum value
List max_heap = new List();
// Stores the maximum profit
int maxProfit = 0;
// Traverse the array and push
// all the elements in max_heap
for(int i = 0; i < N; i++)
max_heap.Add(arr[i]);
max_heap.Sort();
max_heap.Reverse();
// Iterate a loop until M > 0
while (M > 0)
{
// Decrement the value
// of M by 1
M--;
// Pop the maximum element
// from the heap
int X = max_heap[0];
max_heap.RemoveAt(0);
// Update the maxProfit
maxProfit += X;
// Push (X - 1) to max heap
max_heap.Add(X - 1);
max_heap.Sort();
max_heap.Reverse();
}
// Print the result
Console.Write(maxProfit);
}
static void Main() {
int[] arr = { 4, 6 };
int M = 4;
int N = arr.Length;
findMaximumProfit(arr, M, N);
}
}
// This code is contributed by mukesh07.
Javascript
19
时间复杂度: O(M * log(N))
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。