📜  在最多H小时内每小时清空N堆的每小时最少收集的硬币数量

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

给定一个数组arr [] ,它包含代表每个堆中硬币数量的N个整数和一个整数H ,任务是查找每小时必须从单个堆中收集的最小硬币数量,以便所有堆在不到H小时的时间内将其清空。

注意:一个小时内只能从一堆硬币中收集硬币。

例子:

方法:想法是使用二进制搜索。请按照以下步骤解决问题:

  • 初始化一个变量,例如ans ,以存储每小时需要收集的最少数量的硬币。
  • 将变量lowhigh初始化为1,并初始化数组中存在的最大值,以设置执行二进制搜索的范围。
  • 迭代直到从高,然后执行以下步骤:
    • 找到中间2值作为(低+高)/。
    • 遍历数组arr []来查找每小时清除中间硬币以清空所有硬币堆所花费的时间,并检查总时间是否超过H。如果发现错误,则将高电平更新为(K – 1)并将ans更新为K。否则,将低电平更新为(K +1)
  • 完成上述步骤后,输出ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of coins to be collected per hour
// to empty N piles in H hours
int minCollectingSpeed(vector& piles,
                       int H)
{
    // Stores the minimum coins
    // to be removed per hour
    int ans = -1;
 
    int low = 1, high;
 
    // Find the maximum array element
    high = *max_element(piles.begin(),
                        piles.end());
 
    // Perform Binary Search
    while (low <= high)
 
    {
        // Store the mid value of the
        // range in K
        int K = low + (high - low) / 2;
 
        int time = 0;
 
        // Find the total time taken to
        // empty N piles by removing K
        // coins per hour
        for (int ai : piles) {
 
            time += (ai + K - 1) / K;
        }
 
        // If total time does not exceed H
        if (time <= H) {
            ans = K;
            high = K - 1;
        }
 
        // Otherwise
        else {
            low = K + 1;
        }
    }
 
    // Print the required result
    cout << ans;
}
 
// Driver Code
int main()
{
    vector arr = { 3, 6, 7, 11 };
    int H = 8;
 
    // Function Call
    minCollectingSpeed(arr, H);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
     
// Function to find the minimum number
// of coins to be collected per hour
// to empty N piles in H hours
static void minCollectingSpeed(int[] piles,
                               int H)
{
     
    // Stores the minimum coins
    // to be removed per hour
    int ans = -1;
  
    int low = 1, high;
  
    // Find the maximum array element
    high = Arrays.stream(piles).max().getAsInt();
  
    // Perform Binary Search
    while (low <= high)
    {
         
        // Store the mid value of the
        // range in K
        int K = low + (high - low) / 2;
  
        int time = 0;
  
        // Find the total time taken to
        // empty N piles by removing K
        // coins per hour
        for(int ai : piles)
        {
            time += (ai + K - 1) / K;
        }
  
        // If total time does not exceed H
        if (time <= H)
        {
            ans = K;
            high = K - 1;
        }
  
        // Otherwise
        else
        {
            low = K + 1;
        }
    }
  
    // Print the required result
    System.out.print(ans);
}
  
// Driver Code
static public void main(String args[])
{
    int[] arr = { 3, 6, 7, 11 };
    int H = 8;
     
    // Function Call
    minCollectingSpeed(arr, H);
}
}
 
// This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
using System.Collections;
 
class GFG
{
 
  // Function to find the minimum number
  // of coins to be collected per hour
  // to empty N piles in H hours
  static void minCollectingSpeed(int[] piles,
                                 int H)
  {
 
    // Stores the minimum coins
    // to be removed per hour
    int ans = -1;   
    int low = 1, high;    
    Array.Sort(piles);
 
    // Find the maximum array element
    high = piles[piles.Length - 1 ];
 
    // Perform Binary Search
    while (low <= high)
    {
 
      // Store the mid value of the
      // range in K
      int K = low + (high - low) / 2;
 
      int time = 0;
 
      // Find the total time taken to
      // empty N piles by removing K
      // coins per hour
      foreach(int ai in piles)
      {
        time += (ai + K - 1) / K;
      }
 
      // If total time does not exceed H
      if (time <= H)
      {
        ans = K;
        high = K - 1;
      }
 
      // Otherwise
      else
      {
        low = K + 1;
      }
    }
 
    // Print the required result
    Console.Write(ans);
  }
 
  // Driver Code
  static public void Main(string []args)
  {
    int[] arr = { 3, 6, 7, 11 };
    int H = 8;
 
    // Function Call
    minCollectingSpeed(arr, H);
  }
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to find the minimum number
# of coins to be collected per hour
# to empty N piles in H hours
def minCollectingSpeed(piles, H):
     
    # Stores the minimum coins
    # to be removed per hour
    ans = -1
    low = 1
 
    # Find the maximum array element
    high = max(piles)
     
    # Perform Binary Search
    while (low <= high):
         
        # Store the mid value of the
        # range in K
        K = low + (high - low) // 2
 
        time = 0
 
        # Find the total time taken to
        # empty N piles by removing K
        # coins per hour
        for ai in piles:
          time += (ai + K - 1) // K
 
        # If total time does not exceed H
        if (time <= H):
            ans = K
            high = K - 1
 
        # Otherwise
        else:
            low = K + 1
 
    # Prthe required result
    print(ans)
 
# Driver Code
if __name__ == '__main__':
    arr = [3, 6, 7, 11]
    H = 8
 
    # Function Call
    minCollectingSpeed(arr, H)
 
# This code is contributed by  mohit kumar 29


输出:
4

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