📌  相关文章
📜  通过销售至多 M 个产品来最大化利润

📅  最后修改于: 2021-10-26 06:50:50             🧑  作者: Mango

给定两个列表,分别包含产品的成本价格 CP[] 和销售价格 SP[]。任务是通过销售至多“M”个产品来最大化利润。

例子:

方法:

  1. 将每个产品(即SP[i]-CP[i] )的买卖损益存储在一个数组中。
  2. 按降序对该数组进行排序。
  3. 将正值相加至 M 值,因为正值表示利润。
  4. 返回总和。

下面是上述方法的实现:

C++
// C++ implementation of above approach:
#include 
using namespace std;
 
// Function to find profit
int solve(int N, int M, int cp[], int sp[])
{
    int profit[N];
 
    // Calculating profit for each gadget
    for (int i = 0; i < N; i++)
        profit[i] = sp[i] - cp[i];
 
    // sort the profit array in descending order
    sort(profit, profit + N, greater());
 
    // variable to calculate total profit
    int sum = 0;
 
    // check for best M profits
    for (int i = 0; i < M; i++) {
        if (profit[i] > 0)
            sum += profit[i];
        else
            break;
    }
 
    return sum;
}
 
// Driver Code
int main()
{
 
    int N = 5, M = 3;
    int CP[] = { 5, 10, 35, 7, 23 };
    int SP[] = { 11, 10, 0, 9, 19 };
 
    cout << solve(N, M, CP, SP);
 
    return 0;
}


Java
// Java implementation of above approach:
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
 
// Function to find profit
static int solve(int N, int M,
                 int cp[], int sp[])
{
    Integer []profit = new Integer[N];
 
    // Calculating profit for each gadget
    for (int i = 0; i < N; i++)
        profit[i] = sp[i] - cp[i];
 
    // sort the profit array
    // in descending order
    Arrays.sort(profit, Collections.reverseOrder());
 
    // variable to calculate total profit
    int sum = 0;
 
    // check for best M profits
    for (int i = 0; i < M; i++)
    {
        if (profit[i] > 0)
            sum += profit[i];
        else
            break;
    }
 
    return sum;
}
 
// Driver Code
public static void main(String args[])
{
    int N = 5, M = 3;
    int CP[] = { 5, 10, 35, 7, 23 };
    int SP[] = { 11, 10, 0, 9, 19 };
 
    System.out.println(solve(N, M, CP, SP));
}
}
 
// This code is contributed
// by Subhadeep Gupta


Python3
# Python3 implementation
# of above approach
 
# Function to find profit
def solve(N, M, cp, sp) :
     
    # take empty list
    profit = []
     
    # Calculating profit
    # for each gadget
    for i in range(N) :
        profit.append(sp[i] - cp[i])
 
    # sort the profit array
    # in descending order
    profit.sort(reverse = True)
 
    sum = 0
     
    # check for best M profits
    for i in range(M) :
        if profit[i] > 0 :
            sum += profit[i]
        else :
            break
 
    return sum
 
# Driver Code
if __name__ == "__main__" :
 
    N, M = 5, 3
    CP = [5, 10, 35, 7, 23]
    SP = [11, 10, 0, 9, 19]
     
    # function calling
    print(solve(N, M, CP, SP))
     
# This code is contributed
# by ANKITRAI1


C#
// C# implementation of above approach:
using System;
 
class GFG
{
 
// Function to find profit
static int solve(int N, int M,
                 int[] cp, int[] sp)
{
    int[] profit = new int[N];
 
    // Calculating profit for each gadget
    for (int i = 0; i < N; i++)
        profit[i] = sp[i] - cp[i];
 
    // sort the profit array
    // in descending order
    Array.Sort(profit);
    Array.Reverse(profit);
 
    // variable to calculate total profit
    int sum = 0;
 
    // check for best M profits
    for (int i = 0; i < M; i++)
    {
        if (profit[i] > 0)
            sum += profit[i];
        else
            break;
    }
 
    return sum;
}
 
// Driver Code
public static void Main()
{
    int N = 5, M = 3;
    int[] CP = { 5, 10, 35, 7, 23 };
    int[] SP = { 11, 10, 0, 9, 19 };
 
    Console.Write(solve(N, M, CP, SP));
}
}
 
// This code is contributed
// by ChitraNayal


PHP
 0)
            $sum += $profit[$i];
        else
            break;
    }
 
    return $sum;
}
 
// Driver Code
$N = 5;
$M = 3;
$CP = array( 5, 10, 35, 7, 23 );
$SP = array( 11, 10, 0, 9, 19 );
 
echo solve($N, $M, $CP, $SP);
 
// This code is contributed
// by ChitraNayal
?>


Javascript


输出:
8

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程