📜  最大程度地延长P巧克力可以连续分配给N个人的天数

📅  最后修改于: 2021-05-17 04:30:09             🧑  作者: Mango

给定一个整数, P表示巧克力的数量,数组a []其中a i表示第i巧克力的类型。每天有N个人想吃巧克力。考虑以下条件,找到N个人可以连续吃巧克力的最大天数:

  1. N个人每个人在特定的一天必须只吃一种巧克力。
  2. 一个人只能整天吃同一类型的巧克力。

例子:

方法:请按照以下步骤解决问题:

  • 可以分发巧克力的最小天数为0 ,最大数目为P。
  • 因此,对于此范围内的每个数字X ,请检查是否有可能在X天内向每个人分发巧克力。
  • 对于所有这样的X,找到最大值。
  • 现在,使用“二进制搜索”检查范围在0到P之间的所有数字。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Stores the frequency of
// each type of chocolate
map mp;
 
int N, P;
 
// Function to check if chocolates
// can be eaten for 'mid' no. of days
bool helper(int mid)
{
 
    int cnt = 0;
    for (auto i : mp) {
        int temp = i.second;
 
        while (temp >= mid) {
            temp -= mid;
            cnt++;
        }
    }
 
    // If cnt exceeds N,
    // return true
    return cnt >= N;
}
 
// Function to find the maximum
// number of days for which
// chocolates can be eaten
int findMaximumDays(int arr[])
{
 
    // Store the frequency
    // of each type of chocolate
    for (int i = 0; i < P; i++) {
        mp[arr[i]]++;
    }
 
    // Initialize start and end
    // with 0 and P respectively
    int start = 0, end = P, ans = 0;
    while (start <= end) {
 
        // Calculate mid
        int mid = start
                  + ((end - start) / 2);
 
        // Check if chocolates can be
        // distributed for mid days
        if (mid != 0 and helper(mid)) {
 
            ans = mid;
 
            // Check if chocolates can
            // be distributed for more
            // than mid consecutive days
            start = mid + 1;
        }
        else if (mid == 0) {
            start = mid + 1;
        }
        else {
            end = mid - 1;
        }
    }
 
    return ans;
}
 
// Driver code
int main()
{
 
    N = 3, P = 10;
    int arr[] = { 1, 2, 2, 1, 1,
                  3, 3, 3, 2, 4 };
 
    // Function call
    cout << findMaximumDays(arr);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Stores the frequency of
// each type of chocolate
static HashMap mp = new HashMap();
                                          
static int N, P;
 
// Function to check if chocolates
// can be eaten for 'mid' no. of days
static boolean helper(int mid)
{
    int cnt = 0;
    for(Map.Entry i : mp.entrySet())
    {
        int temp = i.getValue();
 
        while (temp >= mid)
        {
            temp -= mid;
            cnt++;
        }
    }
 
    // If cnt exceeds N,
    // return true
    return cnt >= N;
}
 
// Function to find the maximum
// number of days for which
// chocolates can be eaten
static int findMaximumDays(int arr[])
{
     
    // Store the frequency
    // of each type of chocolate
    for(int i = 0; i < P; i++)
    {
        if (mp.containsKey(arr[i]))
        {
            mp.put(arr[i], mp.get(arr[i]) + 1);
        }
        else
        {
            mp.put(arr[i], 1);
        }
    }
 
    // Initialize start and end
    // with 0 and P respectively
    int start = 0, end = P, ans = 0;
    while (start <= end)
    {
         
        // Calculate mid
        int mid = start +
                  ((end - start) / 2);
 
        // Check if chocolates can be
        // distributed for mid days
        if (mid != 0 && helper(mid))
        {
            ans = mid;
 
            // Check if chocolates can
            // be distributed for more
            // than mid consecutive days
            start = mid + 1;
        }
        else if (mid == 0)
        {
            start = mid + 1;
        }
        else
        {
            end = mid - 1;
        }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
     
    N = 3;
    P = 10;
    int arr[] = { 1, 2, 2, 1, 1,
                  3, 3, 3, 2, 4 };
 
    // Function call
    System.out.print(findMaximumDays(arr));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Stores the frequency of
# each type of chocolate
mp = {}
 
N, P = 0, 0
 
# Function to check if chocolates
# can be eaten for 'mid' no. of days
def helper(mid):
 
    cnt = 0;
    for i in mp:
        temp = mp[i]
 
        while (temp >= mid):
            temp -= mid
            cnt += 1
 
    # If cnt exceeds N,
    # return true
    return cnt >= N
 
# Function to find the maximum
# number of days for which
# chocolates can be eaten
def findMaximumDays(arr):
 
    # Store the frequency
    # of each type of chocolate
    for i in range(P):
        mp[arr[i]] = mp.get(arr[i], 0) + 1
 
    # Initialize start and end
    # with 0 and P respectively
    start = 0
    end = P
    ans = 0
     
    while (start <= end):
 
        # Calculate mid
        mid = start + ((end - start) // 2)
 
        # Check if chocolates can be
        # distributed for mid days
        if (mid != 0 and helper(mid)):
            ans = mid
 
            # Check if chocolates can
            # be distributed for more
            # than mid consecutive days
            start = mid + 1
        elif (mid == 0):
            start = mid + 1
        else:
            end = mid - 1
 
    return ans
 
# Driver code
if __name__ == '__main__':
 
    N = 3
    P = 10
     
    arr = [ 1, 2, 2, 1, 1,
            3, 3, 3, 2, 4 ]
 
    # Function call
    print(findMaximumDays(arr))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Stores the frequency of
// each type of chocolate
static Dictionary mp = new Dictionary();                                        
static int N, P;
 
// Function to check if
// chocolates can be eaten
// for 'mid' no. of days
static bool helper(int mid)
{
  int cnt = 0;
  foreach(KeyValuePair i in mp)
  {
    int temp = i.Value;
 
    while (temp >= mid)
    {
      temp -= mid;
      cnt++;
    }
  }
 
  // If cnt exceeds N,
  // return true
  return cnt >= N;
}
 
// Function to find the maximum
// number of days for which
// chocolates can be eaten
static int findMaximumDays(int []arr)
{
  // Store the frequency
  // of each type of chocolate
  for(int i = 0; i < P; i++)
  {
    if (mp.ContainsKey(arr[i]))
    {
      mp[arr[i]] =  mp[arr[i]] + 1;
    }
    else
    {
      mp.Add(arr[i], 1);
    }
  }
 
  // Initialize start and end
  // with 0 and P respectively
  int start = 0, end = P, ans = 0;
  while (start <= end)
  {
    // Calculate mid
    int mid = start +
              ((end - start) / 2);
 
    // Check if chocolates can be
    // distributed for mid days
    if (mid != 0 && helper(mid))
    {
      ans = mid;
 
      // Check if chocolates can
      // be distributed for more
      // than mid consecutive days
      start = mid + 1;
    }
    else if (mid == 0)
    {
      start = mid + 1;
    }
    else
    {
      end = mid - 1;
    }
  }
  return ans;
}
 
// Driver code
public static void Main(String[] args)
{
  N = 3;
  P = 10;
  int []arr = {1, 2, 2, 1, 1,
               3, 3, 3, 2, 4};
 
  // Function call
  Console.Write(findMaximumDays(arr));
}
}
 
// This code is contributed by 29AjayKumar


输出
3

时间复杂度: O(N * log N)
辅助空间: O(N)