给定一个大小为N的数组arr[] ,其中数组中的每个索引代表购买一件商品的成本和两个数字P, K 。任务是找到可以购买的最大数量的物品,使得:
- 如果从数组中购买了第 i 个对象,则剩余数量变为P – arr[i] 。
- 我们可以一次购买K件物品,不一定是连续的,只需支付其中成本最高的物品即可。现在,剩余的金额将是P – max(K 个项目的成本) 。
例子:
Input: arr[] = {2, 4, 3, 5, 7}, P = 6, K = 2
Output: 3
Explanation:
We can buy the first item whose cost is 2. So, the remaining amount is P = 6 – 2 = 4.
Now, we can choose the second and third item and pay for the maximum one which is max(4, 3) = 4, and the remaining amount is 4 – 4 = 0.
Therefore, the total number of items bought is 3.
Input: arr[] = {2, 4, 3, 5, 7}, P = 11, K = 2
Output: 4
Explanation:
We can buy the first and third item together and pay for only the maximum one which is max(2, 3) = 3. The remaining amount is P = 11 – 3 = 8.
Now, we can buy the second and fourth item and pay for the maximum one which is max(4, 5) = 5. The remaining amount is P = 8 – 5 = 3. Now, we cant buy any item further.
做法:思路是利用排序和前缀求和数组的概念。
- 对给定的数组 arr[] 进行排序。
- 查找数组 arr[] 的前缀和。
- 排序背后的想法是,只有当我们购买成本较低的物品时,才能购买最大数量的物品。这种类型的算法被称为贪婪算法。
- 并且,我们使用前缀 sum 数组来查找购买物品的成本。
下面是上述方法的实现:
C++
// C++ program to find the
// maximum number of items
// that can be bought from
// the given cost array
#include
using namespace std;
// Function to find the
// maximum number of items
// that can be bought from
// the given cost array
int number(int a[], int n, int p, int k)
{
// Sort the given array
sort(a, a + n);
// Variables to store the prefix
// sum, answer and the counter
// variables
int pre[n] = { 0 }, val, i,
j, ans = 0;
// Initializing the first element
// of the prefix array
pre[0] = a[0];
// If we can buy at least one item
if (pre[0] <= p)
ans = 1;
// Iterating through the first
// K items and finding the
// prefix sum
for (i = 1; i < k - 1; i++) {
pre[i] = pre[i - 1] + a[i];
// Check the number of items
// that can be bought
if (pre[i] <= p)
ans = i + 1;
}
pre[k - 1] = a[k - 1];
// Finding the prefix sum for
// the remaining elements
for (i = k - 1; i < n; i++) {
if (i >= k) {
pre[i] += pre[i - k] + a[i];
}
// Check the number of iteams
// that can be bought
if (pre[i] <= p)
ans = i + 1;
}
return ans;
}
// Driver code
int main()
{
int n = 5;
int arr[] = { 2, 4, 3, 5, 7 };
int p = 11;
int k = 2;
cout << number(arr, n, p, k) << endl;
return 0;
}
Java
// Java program to find the maximum
// number of items that can be bought
// from the given cost array
import java.io.*;
import java.util.*;
class GFG{
// Function to find the
// maximum number of items
// that can be bought from
// the given cost array
static int number(int[] a, int n,
int p, int k)
{
// Sort the given array
Arrays.sort(a);
// Variables to store the prefix
// sum, answer and the counter
// variables
int[] pre = new int[n];
int val, i, j, ans = 0;
// Initializing the first element
// of the prefix array
pre[0] = a[0];
// If we can buy at least one item
if (pre[0] <= p)
ans = 1;
// Iterating through the first
// K items and finding the
// prefix sum
for(i = 1; i < k - 1; i++)
{
pre[i] = pre[i - 1] + a[i];
// Check the number of items
// that can be bought
if (pre[i] <= p)
ans = i + 1;
}
pre[k - 1] = a[k - 1];
// Finding the prefix sum for
// the remaining elements
for(i = k - 1; i < n; i++)
{
if (i >= k)
{
pre[i] += pre[i - k] + a[i];
}
// Check the number of iteams
// that can be bought
if (pre[i] <= p)
ans = i + 1;
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int n = 5;
int[] arr = { 2, 4, 3, 5, 7 };
int p = 11;
int k = 2;
System.out.println(number(arr, n, p, k));
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program to find the maximum
# number of items that can be bought
# from the given cost array
# Function to find the maximum
# number of items that can be
# bought from the given cost array
def number(a, n, p, k):
# Sort the given array
a.sort()
# Variables to store the prefix
# sum, answer and the counter
# variables
pre = [ ]
for i in range(n):
pre.append(0)
ans = 0
val = 0
i = 0
j = 0
# Initializing the first element
# of the prefix array
pre[0] = a[0]
# If we can buy at least one item
if pre[0] <= p:
ans = 1
# Iterating through the first
# K items and finding the
# prefix sum
for i in range(1, k - 1):
pre[i] = pre[i - 1] + a[i]
# Check the number of items
# that can be bought
if pre[i] <= p:
ans = i + 1
pre[k - 1] = a[k - 1]
# Finding the prefix sum for
# the remaining elements
for i in range(k - 1, n):
if i >= k:
pre[i] += pre[i - k] + a[i]
# Check the number of iteams
# that can be bought
if pre[i] <= p:
ans = i+ 1
return ans
# Driver code
n = 5
arr = [ 2, 4, 3, 5, 7 ]
p = 11
k = 2
print(number(arr, n, p, k))
# This code is contributed by ishayadav181
C#
// C# program to find the maximum
// number of items that can be
// bought from the given cost array
using System;
using System.Collections;
class GFG{
// Function to find the
// maximum number of items
// that can be bought from
// the given cost array
static int number(int[] a, int n,
int p, int k)
{
// Sort the given array
Array.Sort(a);
// Variables to store the prefix
// sum, answer and the counter
// variables
int[] pre = new int[n];
int i, ans = 0;
// Initializing the first element
// of the prefix array
pre[0] = a[0];
// If we can buy at least one item
if (pre[0] <= p)
ans = 1;
// Iterating through the first
// K items and finding the
// prefix sum
for(i = 1; i < k - 1; i++)
{
pre[i] = pre[i - 1] + a[i];
// Check the number of items
// that can be bought
if (pre[i] <= p)
ans = i + 1;
}
pre[k - 1] = a[k - 1];
// Finding the prefix sum for
// the remaining elements
for(i = k - 1; i < n; i++)
{
if (i >= k)
{
pre[i] += pre[i - k] + a[i];
}
// Check the number of iteams
// that can be bought
if (pre[i] <= p)
ans = i + 1;
}
return ans;
}
// Driver code
static public void Main ()
{
int n = 5;
int[] arr = { 2, 4, 3, 5, 7 };
int p = 11;
int k = 2;
Console.WriteLine(number(arr, n, p, k));
}
}
// This code is contributed by akhilsaini
Javascript
4
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。