📌  相关文章
📜  最大化通过在N个购买者中出售商品而获得的利润

📅  最后修改于: 2021-04-17 18:49:26             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是找到商品的价格,以使在N个买家中出售商品所获得的利润最大可能由N个买家的预算组成。如果买方的预算大于或等于该项目的价格,则该项目可以出售给任何买方。

例子:

天真的方法:请按照以下步骤解决问题:

  • 初始化两个变量,例如priceprofit0 ,以分别通过出售一件商品和该商品的可能价格来存储利润。
  • 遍历给定数组arr []并执行以下步骤:
    • 将项目的价格设置为arr [i]
    • 通过遍历给定数组,找到预算至少为arr [i]的购买者数量。让该值计数
    • 如果数值*改编[i]是不是更大的盈利,然后更新利润数*改编[i]价格作为改编[1]。
  • 完成上述步骤后,打印价格值作为结果价格。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#include 
#include 
using namespace std;
 
// Function to find the maximum profit
// earned by selling an item among
// N buyers
int maximumProfit(int arr[],int N)
{
 
  // Stores the maximum profit
  int ans = INT_MIN;
 
  // Stores the price of the item
  int price = 0;
 
  // Sort the array
  sort(arr, arr + N);
 
  // Traverse the array
  for (int i = 0; i < N; i++)
  {
 
    // Count of buyers with
    // budget >= arr[i]
    int count = (N - i);
 
    // Update the maximum profit
    if (ans < count * arr[i])
    {
      price = arr[i];
      ans = count * arr[i];
    }
  }
 
  // Return the maximum possible
  // price
  return price;
}
 
// Driver code
int main()
{
  int arr[] = { 22, 87, 9, 50, 56, 43 };
  cout << maximumProfit(arr,6);
  return 0;
}
 
// This code is contributed by le0.


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to find the maximum profit
    // earned by selling an item among
    // N buyers
    public static int maximumProfit(int arr[])
    {
        // Stores the maximum profit
        int ans = Integer.MIN_VALUE;
 
        // Stores the price of the item
        int price = 0;
 
        int n = arr.length;
 
        // Traverse the array
        for (int i = 0; i < n; i++) {
 
            // Count of buyers with
            // budget >= arr[i]
            int count = 0;
 
            for (int j = 0; j < n; j++) {
 
                if (arr[i] <= arr[j]) {
 
                    // Increment count
                    count++;
                }
            }
 
            // Update the maximum profit
            if (ans < count * arr[i]) {
                price = arr[i];
                ans = count * arr[i];
            }
        }
 
        // Return the maximum possible
        // price
        return price;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 22, 87, 9, 50, 56, 43 };
        System.out.print(
            maximumProfit(arr));
    }
}


Python3
# Python3 program for the above approach
import sys
 
# Function to find the maximum profit
# earned by selling an item among
# N buyers
def maximumProfit(arr, N):
 
    # Stores the maximum profit
    ans = -sys.maxsize - 1
 
    # Stores the price of the item
    price = 0
 
    # Sort the array
    arr.sort()
 
    # Traverse the array
    for i in range(N):
 
        # Count of buyers with
        # budget >= arr[i]
        count = (N - i)
 
        # Update the maximum profit
        if (ans < count * arr[i]):
 
            price = arr[i]
            ans = count * arr[i]
 
    # Return the maximum possible
    # price
    return price
 
# Driver code
if __name__ == "__main__":
 
    arr = [22, 87, 9, 50, 56, 43]
     
    print(maximumProfit(arr, 6))
 
# This code is contributed by ukasp


C#
// C# program for the above approach
using System;
 
class GFG{
 
  // Function to find the maximum profit
  // earned by selling an item among
  // N buyers
  public static int maximumProfit(int[] arr)
  {
     
    // Stores the maximum profit
    int ans = Int32.MinValue;
 
    // Stores the price of the item
    int price = 0;
 
    int n = arr.Length;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
      // Count of buyers with
      // budget >= arr[i]
      int count = 0;
 
      for (int j = 0; j < n; j++) {
 
        if (arr[i] <= arr[j]) {
 
          // Increment count
          count++;
        }
      }
 
      // Update the maximum profit
      if (ans < count * arr[i]) {
        price = arr[i];
        ans = count * arr[i];
      }
    }
 
    // Return the maximum possible
    // price
    return price;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int[] arr = { 22, 87, 9, 50, 56, 43 };
    Console.Write(
      maximumProfit(arr));
  }
}
 
// This code is contributed by sanjoy_62.


C++
#include 
#include 
#include 
using namespace std;
// Function to find the maximum profit
// earned by selling an item among
// N buyers
int maximumProfit(int arr[],int n)
{
  // Stores the maximum profit
  int ans = INT_MIN;
 
  // Stores the price of the item
  int price = 0;
 
 
  // Traverse the array
  for (int i = 0; i < n; i++) {
 
    // Count of buyers with
    // budget >= arr[i]
    int count = 0;
 
    for (int j = 0; j < n; j++) {
 
      if (arr[i] <= arr[j]) {
 
        // Increment count
        count++;
      }
    }
 
    // Update the maximum profit
    if (ans < count * arr[i]) {
      price = arr[i];
      ans = count * arr[i];
    }
  }
 
  // Return the maximum possible
  // price
  return price;
}
 
// Driver code
int main()
{
 
  int arr[] = { 22, 87, 9, 50, 56, 43 };
 
  cout<


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to find the maximum profit
    // earned by selling an item among
    // N buyers
    public static int maximumProfit(int arr[])
    {
        // Stores the maximum profit
        int ans = Integer.MIN_VALUE;
 
        // Stores the price of the item
        int price = 0;
 
        // Sort the array
        Arrays.sort(arr);
 
        int N = arr.length;
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
 
            // Count of buyers with
            // budget >= arr[i]
            int count = (N - i);
 
            // Update the maximum profit
            if (ans < count * arr[i]) {
                price = arr[i];
                ans = count * arr[i];
            }
        }
 
        // Return the maximum possible
        // price
        return price;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 22, 87, 9, 50, 56, 43 };
 
        System.out.print(
            maximumProfit(arr));
    }
}


C#
// C# Program to implement
// the above approach
using System;
class GFG
{
 
  // Function to find the maximum profit
  // earned by selling an item among
  // N buyers
  public static int maximumProfit(int[] arr)
  {
     
    // Stores the maximum profit
    int ans = Int32.MinValue;
 
    // Stores the price of the item
    int price = 0;
 
    // Sort the array
    Array.Sort(arr);
 
    int N = arr.Length;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // Count of buyers with
      // budget >= arr[i]
      int count = (N - i);
 
      // Update the maximum profit
      if (ans < count * arr[i]) {
        price = arr[i];
        ans = count * arr[i];
      }
    }
 
    // Return the maximum possible
    // price
    return price;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int[] arr = { 22, 87, 9, 50, 56, 43 };
 
    Console.WriteLine(
      maximumProfit(arr));
  }
}
 
// This code is contributed by splevel62.


Python3
import sys
 
# Function to find the maximum profit
# earned by selling an item among
# N buyers
def maximumProfit(arr, n):
   
    # Stores the maximum profit
    ans = -sys.maxsize - 1
     
    # Stores the price of the item
    price = 0
 
    # Traverse the array
    for i in range(n):
       
        # Count of buyers with
        # budget >= arr[i]
        count = 0
 
        for j in range(n):
            if (arr[i] <= arr[j]):
               
                # Increment count
                count += 1
 
        # Update the maximum profit
        if (ans < count * arr[i]):
            price = arr[i]
            ans = count * arr[i]
 
    # Return the maximum possible
    # price
    return price;
 
# Driver code
if __name__ == '__main__':
    arr =  [22, 87, 9, 50, 56, 43]
    print(maximumProfit(arr,6))
 
    # This code is contributed by SURENDRA_GANGWAR.


输出:
43

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

高效方法:可以通过对数组进行排序来优化上述方法,以便可以在O(1)时间内计算大于当前元素的元素数。请按照以下步骤解决问题:

  • 初始化两个变量,例如priceprofit0 ,以分别通过出售一件商品和该商品的可能价格来存储利润。
  • 以升序对数组进行排序。
  • 遍历给定数组arr [i]并执行以下步骤:
    • 将项目的价格设置为arr [i]
    • 现在,预算至少为arr [i]的购买者数量由(N – i)给出。让该值计数
    • 如果数值*改编[i]是不是更大的盈利,然后更新利润数*改编[i]价格作为改编[1]。
  • 完成上述步骤后,打印价格值作为结果价格。

下面是上述方法的实现:

C++

#include 
#include 
#include 
using namespace std;
// Function to find the maximum profit
// earned by selling an item among
// N buyers
int maximumProfit(int arr[],int n)
{
  // Stores the maximum profit
  int ans = INT_MIN;
 
  // Stores the price of the item
  int price = 0;
 
 
  // Traverse the array
  for (int i = 0; i < n; i++) {
 
    // Count of buyers with
    // budget >= arr[i]
    int count = 0;
 
    for (int j = 0; j < n; j++) {
 
      if (arr[i] <= arr[j]) {
 
        // Increment count
        count++;
      }
    }
 
    // Update the maximum profit
    if (ans < count * arr[i]) {
      price = arr[i];
      ans = count * arr[i];
    }
  }
 
  // Return the maximum possible
  // price
  return price;
}
 
// Driver code
int main()
{
 
  int arr[] = { 22, 87, 9, 50, 56, 43 };
 
  cout<

Java

// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to find the maximum profit
    // earned by selling an item among
    // N buyers
    public static int maximumProfit(int arr[])
    {
        // Stores the maximum profit
        int ans = Integer.MIN_VALUE;
 
        // Stores the price of the item
        int price = 0;
 
        // Sort the array
        Arrays.sort(arr);
 
        int N = arr.length;
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
 
            // Count of buyers with
            // budget >= arr[i]
            int count = (N - i);
 
            // Update the maximum profit
            if (ans < count * arr[i]) {
                price = arr[i];
                ans = count * arr[i];
            }
        }
 
        // Return the maximum possible
        // price
        return price;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 22, 87, 9, 50, 56, 43 };
 
        System.out.print(
            maximumProfit(arr));
    }
}

C#

// C# Program to implement
// the above approach
using System;
class GFG
{
 
  // Function to find the maximum profit
  // earned by selling an item among
  // N buyers
  public static int maximumProfit(int[] arr)
  {
     
    // Stores the maximum profit
    int ans = Int32.MinValue;
 
    // Stores the price of the item
    int price = 0;
 
    // Sort the array
    Array.Sort(arr);
 
    int N = arr.Length;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
      // Count of buyers with
      // budget >= arr[i]
      int count = (N - i);
 
      // Update the maximum profit
      if (ans < count * arr[i]) {
        price = arr[i];
        ans = count * arr[i];
      }
    }
 
    // Return the maximum possible
    // price
    return price;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int[] arr = { 22, 87, 9, 50, 56, 43 };
 
    Console.WriteLine(
      maximumProfit(arr));
  }
}
 
// This code is contributed by splevel62.

Python3

import sys
 
# Function to find the maximum profit
# earned by selling an item among
# N buyers
def maximumProfit(arr, n):
   
    # Stores the maximum profit
    ans = -sys.maxsize - 1
     
    # Stores the price of the item
    price = 0
 
    # Traverse the array
    for i in range(n):
       
        # Count of buyers with
        # budget >= arr[i]
        count = 0
 
        for j in range(n):
            if (arr[i] <= arr[j]):
               
                # Increment count
                count += 1
 
        # Update the maximum profit
        if (ans < count * arr[i]):
            price = arr[i]
            ans = count * arr[i]
 
    # Return the maximum possible
    # price
    return price;
 
# Driver code
if __name__ == '__main__':
    arr =  [22, 87, 9, 50, 56, 43]
    print(maximumProfit(arr,6))
 
    # This code is contributed by SURENDRA_GANGWAR.
输出:
43

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