给定一个数组arr [] ,它包含代表每个堆中硬币数量的N个整数和一个整数H ,任务是查找每小时必须从单个堆中收集的最小硬币数量,以便所有堆在不到H小时的时间内将其清空。
注意:一个小时内只能从一堆硬币中收集硬币。
例子:
Input: arr[] = {3, 6, 7, 11}, H = 8
Output: 4
Explanation:
Removing 4 coins per pile in each hour, the time taken to empty each pile are as follows:
arr[0] = 3: Emptied in 1 hour.
arr[1] = 6: 4 coins removed in the 1st hour and 2 removed in the 2nd hour. Therefore, emptied in 2 hours.
arr[2] = 7: 4 coins removed in the 1st hour and 3 removed in the 2nd hour. Therefore, emptied in 2 hours.
arr[3] = 11: 4 coins removed in both 1st and 2nd hour, and 3 removed in the 3rd hour. Therefore, emptied in 3 hours.
Therefore, number of hours required = 1 + 2 + 2 + 3 = 8 ( = H).
Input: arr[] = {30, 11, 23, 4, 20}, H = 5
Output: 30
方法:想法是使用二进制搜索。请按照以下步骤解决问题:
- 初始化一个变量,例如ans ,以存储每小时需要收集的最少数量的硬币。
- 将变量low和high初始化为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)