📜  将 P 块巧克力连续分发给 N 人的天数最大化

📅  最后修改于: 2021-09-06 06:25:52             🧑  作者: Mango

给定的整数,P表示巧克力的数目和阵列的[]其中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


Javascript


输出
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live