📌  相关文章
📜  找出购买所有 N 个糖果的最小和最大数量

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

在糖果店里,有 N 种不同类型的糖果可供选择,并且提供了所有 N 种不同类型糖果的价格。糖果店也提供有吸引力的优惠。我们可以从商店购买一个糖果,最多可以免费获得 K 个其他糖果(都是不同类型的)。

  1. 找出购买所有 N 种不同糖果所必须花费的最低金额。
  2. 找出购买所有 N 种不同糖果必须花费的最大金额。

在这两种情况下,我们都必须利用报价并尽可能多地取回糖果。如果有 k 个或更多糖果可用,我们必须为每次购买糖果取 k 个糖果。如果可用的糖果少于 k 个,我们必须将所有糖果都用于购买糖果。

例子:

Input :  
price[] = {3, 2, 1, 4}
k = 2
Output :  
Min = 3, Max = 7
Explanation :
Since k is 2, if we buy one candy we can take 
atmost two more for free.
So in the first case we buy the candy which 
costs 1 and take candies worth 3 and 4 for 
free, also you buy candy worth 2 as well.
So min cost = 1 + 2 = 3.
In the second case we buy the candy which 
costs 4 and take candies worth 1 and 2 for 
free, also We buy candy worth 3 as well.
So max cost = 3 + 4 = 7.

需要注意的一件重要事情是,我们必须使用该优惠,并为每次购买糖果获得最多的糖果。所以如果我们想把钱最小化,我们必须以最低成本购买糖果,免费获得最高成本的糖果。为了使金钱最大化,我们必须反过来做。下面是一个基于此的算法。

First Sort the price array.

For finding minimum amount :
  Start purchasing candies from starting 
  and reduce k free candies from last with
  every single purchase.

For finding maximum amount : 
   Start purchasing candies from the end 
   and reduce k free candies from starting 
   in every single purchase.

下图是上述方法的说明:

最低金额 :

最大金额:

下面是上述方法的实现:

C++
// C++ implementation to find the minimum
// and maximum amount
#include 
using namespace std;
 
// Function to find the minimum amount
// to buy all candies
int findMinimum(int arr[], int n, int k)
{
    int res = 0;
    for (int i = 0; i < n; i++) {
        // Buy current candy
        res += arr[i];
 
        // And take k candies for free
        // from the last
        n = n - k;
    }
    return res;
}
 
// Function to find the maximum amount
// to buy all candies
int findMaximum(int arr[], int n, int k)
{
    int res = 0, index = 0;
 
    for (int i = n - 1; i >= index; i--)
    {
        // Buy candy with maximum amount
        res += arr[i];
 
        // And get k candies for free from
        // the starting
        index += k;
    }
    return res;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 2, 1, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    sort(arr, arr + n);
 
    // Function call
    cout << findMinimum(arr, n, k) << " "
         << findMaximum(arr, n, k) << endl;
    return 0;
}


Java
// Java implementation to find the
// minimum and maximum amount
import java.util.*;
 
class GFG {
 
    // Function to find the minimum
    // amount to buy all candies
    static int findMinimum(int arr[], int n, int k)
    {
        int res = 0;
        for (int i = 0; i < n; i++) {
            // Buy current candy
            res += arr[i];
 
            // And take k candies for free
            // from the last
            n = n - k;
        }
        return res;
    }
 
    // Function to find the maximum
    // amount to buy all candies
    static int findMaximum(int arr[], int n, int k)
    {
        int res = 0, index = 0;
 
        for (int i = n - 1; i >= index; i--)
        {
            // Buy candy with maximum amount
            res += arr[i];
 
            // And get k candies for free from
            // the starting
            index += k;
        }
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 3, 2, 1, 4 };
        int n = arr.length;
        int k = 2;
        Arrays.sort(arr);
 
        // Function call
        System.out.println(findMinimum(arr, n, k) + " "
                           + findMaximum(arr, n, k));
    }
}
 
// This code is contributed by prerna saini


Python3
# Python implementation
# to find the minimum
# and maximum amount
 
# Function to find
# the minimum amount
# to buy all candies
 
 
def findMinimum(arr, n, k):
 
    res = 0
    i = 0
    while(n):
 
        # Buy current candy
        res += arr[i]
 
        # And take k
        # candies for free
        # from the last
        n = n-k
        i += 1
    return res
 
# Function to find
# the maximum amount
# to buy all candies
 
 
def findMaximum(arr, n, k):
 
    res = 0
    index = 0
    i = n-1
    while(i >= index):
 
        # Buy candy with
        # maximum amount
        res += arr[i]
 
        # And get k candies
        # for free from
        # the starting
        index += k
        i -= 1
 
    return res
 
# Driver code
arr = [3, 2, 1, 4]
n = len(arr)
k = 2
 
arr.sort()
 
# Function call
print(findMinimum(arr, n, k), " ",
      findMaximum(arr, n, k))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# implementation to find the
// minimum and maximum amount
using System;
 
public class GFG {
 
    // Function to find the minimum
    // amount to buy all candies
    static int findMinimum(int[] arr, int n, int k)
    {
        int res = 0;
        for (int i = 0; i < n; i++)
        {
 
            // Buy current candy
            res += arr[i];
 
            // And take k candies for
            // free from the last
            n = n - k;
        }
 
        return res;
    }
 
    // Function to find the maximum
    // amount to buy all candies
    static int findMaximum(int[] arr, int n, int k)
    {
        int res = 0, index = 0;
 
        for (int i = n - 1; i >= index; i--)
        {
            // Buy candy with maximum
            // amount
            res += arr[i];
 
            // And get k candies for free
            // from the starting
            index += k;
        }
 
        return res;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 3, 2, 1, 4 };
        int n = arr.Length;
        int k = 2;
        Array.Sort(arr);
 
        // Function call
        Console.WriteLine(findMinimum(arr, n, k) + " "
                          + findMaximum(arr, n, k));
    }
}
 
// This code is contributed by Sam007.


PHP
= $index; $i--)
    {
         
        // Buy candy with maximum amount
        $res += $arr[$i];
 
        // And get k candies
        // for free from
        // the starting
        $index += $k;
    }
    return $res;
}
 
    // Driver Code
    $arr = array(3, 2, 1, 4);
    $n = sizeof($arr);
    $k = 2;
    sort($arr); sort($arr,$n);
 
    // Function call
    echo findMinimum($arr, $n, $k)," "
            ,findMaximum($arr, $n, $k);
    return 0;
 
// This code is contributed by nitin mittal.
?>


Javascript


C++
// C++ implementation
// to find the minimum
// and maximum amount
#include 
using namespace std;
 
// function to find the maximum
// and the minimum cost required
void find(vector arr, int n, int k)
{
 
    // Sort the array
    sort(arr.begin(), arr.end());
    int b = ceil(n / k * 1.0);
    int min_sum = 0, max_sum = 0;
 
    for(int i = 0; i < b; i++)
      min_sum += arr[i];
    for(int i = 2; i < arr.size(); i++)
      max_sum += arr[i];
 
    // print the minimum cost
    cout << "minimum " << min_sum << endl;
 
    // print the maximum cost
    cout << "maximum " << max_sum << endl;
 
}
 
 
// Driver code
int main()
{
  vector arr = {3, 2, 1, 4};
  int n = arr.size();
  int k = 2;
 
  // Function call
  find(arr,n,k);
}
 
// This code is contributed by mohit kumar 29.


Java
// Java implementation to find the minimum
// and maximum amount
import java.io.*;
import java.util.Arrays;
import java.lang.Math;
 
class GFG{
 
// Function to find the maximum
// and the minimum cost required
static void find(int[] arr, int n, int k)
{
     
    // Sort the array
    Arrays.sort(arr);
    int b = (int)Math.ceil(n / k * 1.0);
    int min_sum = 0, max_sum = 0;
 
    for(int i = 0; i < b; i++)
        min_sum += arr[i];
    for(int i = 2; i < arr.length; i++)
        max_sum += arr[i];
 
    // Print the minimum cost
    System.out.println("minimum " + min_sum);
 
    // Print the maximum cost
    System.out.println("maximum " + max_sum);
}
 
// Driver code
public static void main (String[] args)
{
    int[] arr = { 3, 2, 1, 4 };
    int n = arr.length;
    int k = 2;
 
    // Function call
    find(arr, n, k);
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python implementation
# to find the minimum
# and maximum amount
 
#import ceil function
from math import ceil
 
# function to find the maximum
# and the minimum cost required
def find(arr,n,k):
     
    # Sort the array
    arr.sort()
    b = int(ceil(n/k))
     
    # print the minimum cost
    print("minimum ",sum(arr[:b]))
     
    # print the maximum cost
    print("maximum ", sum(arr[-b:]))
     
     
# Driver Code
arr = [3, 2, 1, 4]
n = len(arr)
k = 2
 
# Function call
find(arr,n,k)


C#
// C# implementation to find the minimum
// and maximum amount
using System;
 
class GFG{
 
// Function to find the maximum
// and the minimum cost required
static void find(int[] arr, int n, int k)
{
     
    // Sort the array
    Array.Sort(arr);
    int b = (int)Math.Ceiling(n / k * 1.0);
    int min_sum = 0, max_sum = 0;
 
    for(int i = 0; i < b; i++)
        min_sum += arr[i];
    for(int i = 2; i < arr.Length; i++)
        max_sum += arr[i];
 
    // Print the minimum cost
    Console.WriteLine("minimum " + min_sum);
 
    // Print the maximum cost
    Console.WriteLine("maximum " + max_sum);
}
 
// Driver code
public static void Main()
{
    int[] arr = { 3, 2, 1, 4 };
    int n = arr.Length;
    int k = 2;
 
    // Function call
    find(arr, n, k);
}
}
 
// This code is contributed by ukasp


Javascript


输出
3 7

时间复杂度: O(n log n)

另一个实现:
我们可以借助内置的 ceil()函数,借助最小整数函数(Ceiling函数)来实现:

下面是在Python的实现:

C++

// C++ implementation
// to find the minimum
// and maximum amount
#include 
using namespace std;
 
// function to find the maximum
// and the minimum cost required
void find(vector arr, int n, int k)
{
 
    // Sort the array
    sort(arr.begin(), arr.end());
    int b = ceil(n / k * 1.0);
    int min_sum = 0, max_sum = 0;
 
    for(int i = 0; i < b; i++)
      min_sum += arr[i];
    for(int i = 2; i < arr.size(); i++)
      max_sum += arr[i];
 
    // print the minimum cost
    cout << "minimum " << min_sum << endl;
 
    // print the maximum cost
    cout << "maximum " << max_sum << endl;
 
}
 
 
// Driver code
int main()
{
  vector arr = {3, 2, 1, 4};
  int n = arr.size();
  int k = 2;
 
  // Function call
  find(arr,n,k);
}
 
// This code is contributed by mohit kumar 29.

Java

// Java implementation to find the minimum
// and maximum amount
import java.io.*;
import java.util.Arrays;
import java.lang.Math;
 
class GFG{
 
// Function to find the maximum
// and the minimum cost required
static void find(int[] arr, int n, int k)
{
     
    // Sort the array
    Arrays.sort(arr);
    int b = (int)Math.ceil(n / k * 1.0);
    int min_sum = 0, max_sum = 0;
 
    for(int i = 0; i < b; i++)
        min_sum += arr[i];
    for(int i = 2; i < arr.length; i++)
        max_sum += arr[i];
 
    // Print the minimum cost
    System.out.println("minimum " + min_sum);
 
    // Print the maximum cost
    System.out.println("maximum " + max_sum);
}
 
// Driver code
public static void main (String[] args)
{
    int[] arr = { 3, 2, 1, 4 };
    int n = arr.length;
    int k = 2;
 
    // Function call
    find(arr, n, k);
}
}
 
// This code is contributed by shivanisinghss2110

蟒蛇3

# Python implementation
# to find the minimum
# and maximum amount
 
#import ceil function
from math import ceil
 
# function to find the maximum
# and the minimum cost required
def find(arr,n,k):
     
    # Sort the array
    arr.sort()
    b = int(ceil(n/k))
     
    # print the minimum cost
    print("minimum ",sum(arr[:b]))
     
    # print the maximum cost
    print("maximum ", sum(arr[-b:]))
     
     
# Driver Code
arr = [3, 2, 1, 4]
n = len(arr)
k = 2
 
# Function call
find(arr,n,k)

C#

// C# implementation to find the minimum
// and maximum amount
using System;
 
class GFG{
 
// Function to find the maximum
// and the minimum cost required
static void find(int[] arr, int n, int k)
{
     
    // Sort the array
    Array.Sort(arr);
    int b = (int)Math.Ceiling(n / k * 1.0);
    int min_sum = 0, max_sum = 0;
 
    for(int i = 0; i < b; i++)
        min_sum += arr[i];
    for(int i = 2; i < arr.Length; i++)
        max_sum += arr[i];
 
    // Print the minimum cost
    Console.WriteLine("minimum " + min_sum);
 
    // Print the maximum cost
    Console.WriteLine("maximum " + max_sum);
}
 
// Driver code
public static void Main()
{
    int[] arr = { 3, 2, 1, 4 };
    int n = arr.Length;
    int k = 2;
 
    // Function call
    find(arr, n, k);
}
}
 
// This code is contributed by ukasp

Javascript


输出
('minimum ', 3)
('maximum ', 7)

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