Problem Link - Problem-2 Practice Problem in 1600 to 1800 difficulty problems
Problem Statement:
The problem asks us to find the minimum number of laddoos K that Hrishi’s daughters can eat per hour such that they can finish all the laddoos from N sets within H hours.
Approach:
- Basic Insight:
- For each set of laddoos A_i , if the daughters eat
Kladdoos per hour, the number of hours required to finish this set can be computed as: hours_needed (A_i)=(A_i+K−1)/K (integer division).The reason we use the formula is that they cannot eat a fraction of a laddoo set.
- Binary Search for the Minimum K:
- To find the minimum
K, we can perform binary search over possible values ofK. - For each
Kin this range, we compute the total number of hours required to finish all laddoos, and check if it is less than or equal toH.
Algorithm:
-
Feasibility Check: Calculate the total hours needed for a given
K: total_hours = \sum_{i=1}^{N} \frac{A_i + K - 1}{K} the total hours is less than or equal toH, then the valueKis feasible. -
Binary Search:
- Set
l=1andr=max(A). - For each midpoint ‘mid’ in the binary search range, check if it is feasible using the
checkfunction. - If it’s feasible, try smaller values of
Kby settingr=mid−1. - If it’s not feasible, try larger values by setting
l=mid+1.
Complexity:
- Time Complexity: The binary search operates over the range of possible values for
K, which is between1and the maximum number of laddoos in any setO(log(max_laddoos)). For each middle valueKtested in the binary search, the check function iterates through the arrayA, which takesO(N)time. Total time complexity:O(N⋅log(max_laddoos)). - Space Complexity:
O(1)No extra space required.