最多购买K本书可以获得的最大利润
给定一个整数K和一个由N个整数组成的数组arr[] ,其中数组元素arr[i]表示第i本书的价格。购买第 i本书的利润表示max(0, -1 * arr[i]) ,任务是通过最多购买K本书找到可能的最大利润。
例子:
Input: arr[] = {-10, 20, -30, 50, -19}, K = 2
Output: 49
Explanation:
Maximum profit can be obtained by buying the books arr[2](= -30). Profit = 30 and the book, arr[4](= -19) for the profit of 19.
Therefore, the total maximum profit obtained is, (30+19 = 49).
Input: arr[] = {10, 20, 16, 25}, K = 3
Output: 0
方法:可以使用贪心方法解决问题,该方法基于观察到只有负价格的书籍才能获得最大利润。请按照以下步骤解决此问题:
- 按升序对数组 arr[] 进行排序。
- 初始化一个变量,比如maxBenefit为0以存储最大利润。
- 使用变量i在[0, N-1]范围内迭代并执行以下步骤:
- 如果K大于0且arr[i]为负数,则将abs(arr[i])添加到maxBenefit 中,然后将K的值减1。
- 最后,打印maxBenefit的值作为获得的最大利润。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find the maximum
// profit that can be obtained
// by buying at most K books
int maxProfit(int arr[], int N, int K)
{
// Sort the array in
// ascending order
sort(arr, arr + N);
// Stores the maximum profit
int maxBenefit = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If arr[i] is less than 0
// and K is greater than 0
if (arr[i] < 0 && K > 0) {
// Increment the maxBenefit
// by abs(arr[i])
maxBenefit += abs(arr[i]);
// Decrement K by 1
K--;
}
}
// Return the profit
return maxBenefit;
}
// Driver Code
int main()
{
// Given Input
int arr[] = { -10, 20, -30, 50, -19 };
int K = 2;
int N = sizeof(arr) / sizeof(int);
// Function call
cout << maxProfit(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG
{
// Function to find the maximum
// profit that can be obtained
// by buying at most K books
public static int maxProfit(int arr[], int N, int K)
{
// Sort the array in
// ascending order
Arrays.sort(arr);
// Stores the maximum profit
int maxBenefit = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If arr[i] is less than 0
// and K is greater than 0
if (arr[i] < 0 && K > 0) {
// Increment the maxBenefit
// by abs(arr[i])
maxBenefit += Math.abs(arr[i]);
// Decrement K by 1
K--;
}
}
// Return the profit
return maxBenefit;
}
// Driver Code
public static void main(String[] args)
{
// Given input
int arr[] = { -10, 20, -30, 50, -19 };
int K = 2;
int N = 5;
// Function call
int res = maxProfit(arr, N, K);
System.out.println(res);
}
}
// This code is contributed by lokeshpotta20.
Python3
# Python3 program for above approach
# Function to find the maximum
# profit that can be obtained
# by buying at most K books
def maxProfit(arr, N, K):
# Sort the array in
# ascending order
arr.sort()
# Stores the maximum profit
maxBenefit = 0
# Traverse the array arr[]
for i in range(0, N, 1):
# If arr[i] is less than 0
# and K is greater than 0
if (arr[i] < 0 and K > 0):
# Increment the maxBenefit
# by abs(arr[i])
maxBenefit += abs(arr[i])
# Decrement K by 1
K -= 1
# Return the profit
return maxBenefit
# Driver Code
if __name__ == '__main__':
# Given Input
arr = [ -10, 20, -30, 50, -19 ]
K = 2
N = len(arr)
# Function call
print(maxProfit(arr, N, K))
# This code is contributed by SURENDRA_GANGWAR
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the maximum
// profit that can be obtained
// by buying at most K books
public static int maxProfit(int[] arr, int N, int K)
{
// Sort the array in
// ascending order
Array.Sort(arr);
// Stores the maximum profit
int maxBenefit = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If arr[i] is less than 0
// and K is greater than 0
if (arr[i] < 0 && K > 0) {
// Increment the maxBenefit
// by abs(arr[i])
maxBenefit += Math.Abs(arr[i]);
// Decrement K by 1
K--;
}
}
// Return the profit
return maxBenefit;
}
// Driver Code
public static void Main()
{
// Given input
int[] arr = { -10, 20, -30, 50, -19 };
int K = 2;
int N = 5;
// Function call
int res = maxProfit(arr, N, K);
Console.Write(res);
}
}
// This code is contributed by subhammahato348.
Javascript
输出:
49
时间复杂度: O(N*log(N))
辅助空间: O(1)