给定一个数组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 和数组中存在的最大值,以设置执行二分搜索的范围。
- 迭代直到低≤高并执行以下步骤:
- 将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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。