Minimized maximum of Products distributed to any store
Last Updated :
10 Jan, 2024
Given N shopkeepers and an array quantities[], where quantities[i] = number of products of type i. All the products need to be distributed among N shopkeepers such that each shopkeeper can have at most one type of product of any quantity possibly 0. The task is to minimize the maximum number of products that are given to any shopkeeper.
Examples:
Input: n = 6, quantities[] = {11,6}
Output: 3
Explanation: One optimal way is:-
-The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
-The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.
Input: n = 1 , quantities[] = {500}
Output: 500
Explanation : The 500 products of type 0 are distributed to the first store.
Approach: To solve the problem follow the below idea.
This problem can be solved using the Binary Search. We know that binary search can be used when any parameter is monotonically increasing here in this case we can apply the binary search on the quantity parameter with low = 0 and high = max quantity.
Steps to solve the problem:
- Initialize low = 0, high = max quantity, and max_dist to store the result.
- Perform binary search while low <=high
- Check if choosing mid value as quantity to be distributed to each shop, in the validDistribution() traverse the quantities array and see how many shops the mid quantity can be distributed to it should not be > n.
- If the mid value is valid minimize the mid-value and store it in max_dist, change the high to mid-1
- Else if the mid value is invalid change the low to mid+1.
- Return the max_dist
Implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isValidDistributionCheck( int n, int mid,
vector< int > quantities)
{
for ( int i = 0; i < quantities.size(); i++) {
if (quantities[i] % mid == 0)
n -= (quantities[i] / mid);
else
n -= ((quantities[i] / mid) + 1);
}
return n >= 0;
}
int maximizedMinumumQuantity( int n, vector< int > quantities)
{
int low = 1;
int high = *max_element(quantities.begin(),
quantities.end());
int max_dis = INT_MAX;
while (low <= high) {
int mid = low + (high - low) / 2;
if (isValidDistributionCheck(n, mid, quantities)) {
max_dis = min(mid, max_dis);
high = mid - 1;
}
else {
low = mid + 1;
}
}
if (max_dis == INT_MAX)
return -1;
return max_dis;
}
int main()
{
vector< int > quantities = { 11, 6 };
int n = 6;
cout << maximizedMinumumQuantity(n, quantities);
return 0;
}
|
Java
import java.util.*;
public class MaximizedMinimumQuantity {
static boolean isValidDistributionCheck( int n, int mid, List<Integer> quantities) {
for ( int i = 0 ; i < quantities.size(); i++) {
if (quantities.get(i) % mid == 0 )
n -= (quantities.get(i) / mid);
else
n -= ((quantities.get(i) / mid) + 1 );
}
return n >= 0 ;
}
static int maximizedMinimumQuantity( int n, List<Integer> quantities) {
int low = 1 ;
int high = Collections.max(quantities);
int max_dis = Integer.MAX_VALUE;
while (low <= high) {
int mid = low + (high - low) / 2 ;
if (isValidDistributionCheck(n, mid, quantities)) {
max_dis = Math.min(mid, max_dis);
high = mid - 1 ;
}
else {
low = mid + 1 ;
}
}
if (max_dis == Integer.MAX_VALUE)
return - 1 ;
return max_dis;
}
public static void main(String[] args) {
List<Integer> quantities = Arrays.asList( 11 , 6 );
int n = 6 ;
System.out.println(maximizedMinimumQuantity(n, quantities));
}
}
|
Python3
def is_valid_distribution_check(n, mid, quantities):
for i in range ( len (quantities)):
if quantities[i] % mid = = 0 :
n - = quantities[i] / / mid
else :
n - = (quantities[i] / / mid) + 1
return n > = 0
def maximized_minimum_quantity(n, quantities):
low = 1
high = max (quantities)
max_dis = float ( 'inf' )
while low < = high:
mid = low + (high - low) / / 2
if is_valid_distribution_check(n, mid, quantities):
max_dis = min (mid, max_dis)
high = mid - 1
else :
low = mid + 1
if max_dis = = float ( 'inf' ):
return - 1
return max_dis
if __name__ = = "__main__" :
quantities = [ 11 , 6 ]
n = 6
print (maximized_minimum_quantity(n, quantities))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static bool IsValidDistributionCheck( int n, int mid, List< int > quantities)
{
foreach ( int quantity in quantities)
{
if (quantity % mid == 0)
n -= (quantity / mid);
else
n -= ((quantity / mid) + 1);
}
return n >= 0;
}
static int MaximizedMinumumQuantity( int n, List< int > quantities)
{
int low = 1;
int high = quantities.Max();
int max_dis = int .MaxValue;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (IsValidDistributionCheck(n, mid, quantities))
{
max_dis = Math.Min(mid, max_dis);
high = mid - 1;
}
else
{
low = mid + 1;
}
}
if (max_dis == int .MaxValue)
return -1;
return max_dis;
}
static void Main( string [] args)
{
List< int > quantities = new List< int > { 11, 6 };
int n = 6;
Console.WriteLine(MaximizedMinumumQuantity(n, quantities));
}
}
|
Javascript
function isValidDistributionCheck(n, mid, quantities) {
for (let i = 0; i < quantities.length; i++) {
if (quantities[i] % mid === 0) {
n -= quantities[i] / mid;
} else {
n -= Math.floor(quantities[i] / mid) + 1;
}
}
return n >= 0;
}
function maximizedMinimumQuantity(n, quantities) {
let low = 1;
let high = Math.max(...quantities);
let max_dis = Infinity;
while (low <= high) {
let mid = low + Math.floor((high - low) / 2);
if (isValidDistributionCheck(n, mid, quantities)) {
max_dis = Math.min(mid, max_dis);
high = mid - 1;
} else {
low = mid + 1;
}
}
if (max_dis === Infinity) {
return -1;
}
return max_dis;
}
const quantities = [11, 6];
const n = 6;
console.log(maximizedMinimumQuantity(n, quantities));
|
Time Complexity: O(M * log N) where M is the size of the array and N is the max value in the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...