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

📅  最后修改于: 2021-09-07 02:23:12             🧑  作者: Mango

给定一个数组arr[]N 个整数组成,代表每堆硬币的数量,以及一个整数H ,任务是找到每小时必须从单个堆中收集的最小硬币数量,这样所有的堆都是在不到H小时内清空。

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

例子:

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

  • 初始化一个变量,比如ans ,以存储每小时需要收集的最小硬币数量。
  • 将变量lowhigh初始化为 1 和数组中存在的最大值,以设置执行二分搜索的范围。
  • 迭代直到低≤高并执行以下步骤:
    • mid的值计算为(low + high)/2
    • 遍历数组arr[]以通过每小时移除中间硬币来查找清空所有硬币堆所需的时间,并检查总时间是否超过H 。如果发现为假,则将 high 更新为(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


Javascript


输出:
4

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

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