给定一个n整数数组,其中每个值代表一个数据包中的巧克力数量。每个小包可以有可变数量的巧克力。有m个学生,任务是分发巧克力小包,以便:
- 每个学生得到一个小包。
- 给学生的巧克力最多的包装和巧克力最少的包装中的巧克力数量之差最小。
例子:
Input : arr[] = {7, 3, 2, 4, 9, 12, 56} , m = 3
Output: Minimum Difference is 2
Explanation:
We have seven packets of chocolates and
we need to pick three packets for 3 students
If we pick 2, 3 and 4, we get the minimum
difference between maximum and minimum packet
sizes.
Input : arr[] = {3, 4, 1, 9, 56, 7, 9, 12} , m = 5
Output: Minimum Difference is 6
Explanation:
The set goes like 3,4,7,9,9 and the output
is 9-3 = 6
Input : arr[] = {12, 4, 7, 9, 2, 23, 25, 41,
30, 40, 28, 42, 30, 44, 48,
43, 50} , m = 7
Output: Minimum Difference is 10
Explanation:
We need to pick 7 packets. We pick 40, 41,
42, 44, 48, 43 and 50 to minimize difference
between maximum and minimum.
资料来源:Flipkart采访经历
一个简单的解决方案是生成arr [0..n-1]的大小为m的所有子集。对于每个子集,找出其中的最大和最小元素之间的差异。最后,返回最小差。
一个有效的解决方案基于以下观察:为使差异最小化,我们必须从已排序的数据包中选择连续的元素。我们首先对数组arr [0..n-1]进行排序,然后找到大小为m的子数组,最后一个元素与第一个元素之间的差最小。
下图是上述方法的模拟:
下面是上述方法的实现:
C++
// C++ program to solve chocolate distribution
// problem
#include
using namespace std;
// arr[0..n-1] represents sizes of packets
// m is number of students.
// Returns minimum difference between maximum
// and minimum values of distribution.
int findMinDiff(int arr[], int n, int m)
{
// if there are no chocolates or number
// of students is 0
if (m == 0 || n == 0)
return 0;
// Sort the given packets
sort(arr, arr + n);
// Number of students cannot be more than
// number of packets
if (n < m)
return -1;
// Largest number of chocolates
int min_diff = INT_MAX;
// Find the subarray of size m such that
// difference between last (maximum in case
// of sorted) and first (minimum in case of
// sorted) elements of subarray is minimum.
for (int i = 0; i + m - 1 < n; i++) {
int diff = arr[i + m - 1] - arr[i];
if (diff < min_diff)
min_diff = diff;
}
return min_diff;
}
int main()
{
int arr[] = { 12, 4, 7, 9, 2, 23, 25, 41, 30,
40, 28, 42, 30, 44, 48, 43, 50 };
int m = 7; // Number of students
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Minimum difference is "
<< findMinDiff(arr, n, m);
return 0;
}
Java
// JAVA Code For Chocolate Distribution
// Problem
import java.util.*;
class GFG {
// arr[0..n-1] represents sizes of
// packets. m is number of students.
// Returns minimum difference between
// maximum and minimum values of
// distribution.
static int findMinDiff(int arr[], int n,
int m)
{
// if there are no chocolates or
// number of students is 0
if (m == 0 || n == 0)
return 0;
// Sort the given packets
Arrays.sort(arr);
// Number of students cannot be
// more than number of packets
if (n < m)
return -1;
// Largest number of chocolates
int min_diff = Integer.MAX_VALUE;
// Find the subarray of size m
// such that difference between
// last (maximum in case of
// sorted) and first (minimum in
// case of sorted) elements of
// subarray is minimum.
for (int i = 0; i + m - 1 < n; i++)
{
int diff = arr[i+m-1] - arr[i];
if (diff < min_diff)
min_diff = diff;
}
return min_diff;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int arr[] = {12, 4, 7, 9, 2, 23,
25, 41, 30, 40, 28,
42, 30, 44, 48, 43,
50};
int m = 7; // Number of students
int n = arr.length;
System.out.println("Minimum difference is "
+ findMinDiff(arr, n, m));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Python3 program to solve
# chocolate distribution
# problem
# arr[0..n-1] represents sizes of packets
# m is number of students.
# Returns minimum difference between maximum
# and minimum values of distribution.
def findMinDiff(arr, n, m):
# if there are no chocolates or number
# of students is 0
if (m==0 or n==0):
return 0
# Sort the given packets
arr.sort()
# Number of students cannot be more than
# number of packets
if (n < m):
return -1
# Largest number of chocolates
min_diff = arr[n-1] - arr[0]
# Find the subarray of size m such that
# difference between last (maximum in case
# of sorted) and first (minimum in case of
# sorted) elements of subarray is minimum.
for i in range(len(arr) - m + 1):
min_diff = min(min_diff , arr[i + m - 1] - arr[i])
return min_diff
# Driver Code
if __name__ == "__main__":
arr = [12, 4, 7, 9, 2, 23, 25, 41,
30, 40, 28, 42, 30, 44, 48,
43, 50]
m = 7 # Number of students
n = len(arr)
print("Minimum difference is", findMinDiff(arr, n, m))
#This code is contributed by Smitha
C#
// C# Code For Chocolate Distribution
// Problem
using System;
class GFG {
// arr[0..n-1] represents sizes of
// packets. m is number of students.
// Returns minimum difference between
// maximum and minimum values of
// distribution.
static int findMinDiff(int []arr, int n,
int m)
{
// if there are no chocolates or
// number of students is 0
if (m == 0 || n == 0)
return 0;
// Sort the given packets
Array.Sort(arr);
// Number of students cannot be
// more than number of packets
if (n < m)
return -1;
// Largest number of chocolates
int min_diff = int.MaxValue;
// Find the subarray of size m
// such that difference between
// last (maximum in case of
// sorted) and first (minimum in
// case of sorted) elements of
// subarray is minimum.
for (int i = 0; i + m - 1 < n; i++)
{
int diff = arr[i+m-1] - arr[i];
if (diff < min_diff)
min_diff = diff;
}
return min_diff;
}
/* Driver program to test above function */
public static void Main()
{
int []arr = {12, 4, 7, 9, 2, 23,
25, 41, 30, 40, 28,
42, 30, 44, 48, 43,
50};
int m = 7; // Number of students
int n = arr.Length;
Console.WriteLine("Minimum difference is "
+ findMinDiff(arr, n, m));
}
}
// This code is contributed by vt_m.
PHP
Javascript
Minimum difference is 10
时间复杂度: O(n Log n),因为我们在子数组搜索之前应用排序。