📌  相关文章
📜  根据给定条件可以从“成本数组”中购买的最大商品

📅  最后修改于: 2021-04-24 23:57:05             🧑  作者: Mango

给定大小为N的数组arr [] ,其中数组中的每个索引代表购买商品的成本以及两个数字P,K 。任务是找到可以购买的最大物品数量,以便:

  1. 如果从数组中购买了第i个对象,则剩余金额变为P – arr [i]
  2. 我们可以一次购买K个商品(不一定是连续的),而只需购买其中最大成本的商品即可。现在,剩余金额将为P – max(K个商品的费用)

例子:

方法:想法是使用排序和前缀和数组的概念。

  • 对给定的数组arr []进行排序。
  • 找到数组arr []的前缀和。
  • 排序背后的想法是,只有当我们以较低的价格购买商品时,才可以购买最大数量的商品。已知这种类型的算法是贪婪算法。
  • 并且,我们使用前缀和数组来查找购买商品的成本。

下面是上述方法的实现:

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


输出:
4