📌  相关文章
📜  通过销售 M 种产品来最大化利润,使得产品的利润是该供应商剩余的产品数量

📅  最后修改于: 2021-10-28 02:13:42             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,其中arr[i]表示第i供应商拥有的产品数量和一个正整数M ,任务是通过销售M产品找到最大利润,如果利润特定产品的数量与该供应商剩余的产品数量相同。

例子:

朴素方法:可以通过从当前剩余产品数量最多的供应商处销售产品来解决给定的问题。所以,思路是迭代一个循环M次,在每次迭代中找到数组中最大元素的值,并将其值添加到利润中,然后将其在数组中的值减1。 循环后,打印利润的价值。

时间复杂度: O(M * N)
辅助空间: O(1)

高效的方法:上述方法也可以通过使用最大堆来优化,以在O(log N)时间内跟踪数组中的最大元素。请按照以下步骤解决问题:

  • 使用优先级队列初始化最大堆,比如Q以跟踪数组中存在的最大元素。
  • 遍历数组arr[]并插入堆Q 中的所有元素。
  • 初始化一个变量,比如maxProfit0来存储获得的结果最大利润。
  • 迭代一个循环直到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 现场工作专业课程学生竞争性编程现场课程