📌  相关文章
📜  最大化通过在 N 个买家中销售一件商品可以获得的利润

📅  最后修改于: 2021-09-07 04:27:29             🧑  作者: Mango

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

例子:

朴素的方法:按照以下步骤解决问题:

  • 初始化两个变量,比如pricemargin0 ,分别存储销售商品的利润和商品的可能价格。
  • 遍历给定的数组arr[]并执行以下步骤:
    • 将商品的价格设置为arr[i]
    • 通过遍历给定数组,找到预算至少为arr[i]的买家数量。让该值为count
    • 如果数值*改编[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.


Javascript


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.


Javascript


输出
43

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

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

  • 初始化两个变量,比如pricemargin0 ,分别存储销售商品的利润和商品的可能价格。
  • 按升序对数组进行排序。
  • 遍历给定的数组arr[i]并执行以下步骤:
    • 将商品的价格设置为arr[i]
    • 现在,预算至少为arr[i]的买家数量由(N – i) 给出。让该值为count
    • 如果数值*改编[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.

蟒蛇3

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.

Javascript


输出
43

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live