-
Notifications
You must be signed in to change notification settings - Fork 0
Minimum Merchandise Distribution Rate
Unit 7 Session 2 (Click for link to problem statements)
- 💡 Difficulty: Hard
- ⏰ Time to complete: 30 mins
- 🛠️ Topics: Binary Search, Greedy Algorithms
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- What should be done if
h
is less than the number of booths?- This scenario is not possible since we need at least one hour per booth.
- Can the distribution rate
r
be fractional?- No,
r
must be an integer as it represents the number of items distributed per hour.
- No,
- What if there is more time than needed to distribute all items?
- The goal is to minimize the distribution rate
r
, even if more time is available.
- The goal is to minimize the distribution rate
HAPPY CASE
Input: booths = [3, 6, 7, 11], h = 8
Output: 4
Explanation: The minimum rate at which you can distribute all items within 8 hours is 4 items/hour.
Input: booths = [30, 11, 23, 4, 20], h = 5
Output: 30
Explanation: You need to distribute at least 30 items/hour to finish within 5 hours.
EDGE CASE
Input: booths = [10, 10, 10], h = 3
Output: 10
Explanation: You need to distribute exactly 10 items/hour to finish within 3 hours.
Input: booths = [1], h = 1
Output: 1
Explanation: You only need to distribute 1 item/hour since there is only one booth and one hour available.
Match what this problem looks like to known categories of problems, e.g., Linked List or Dynamic Programming, and strategies or patterns in those categories.
For problems requiring optimization within constraints, we can consider the following approaches:
-
Binary Search: Binary search can be used to efficiently find the minimum valid distribution rate
r
. - Greedy Algorithm: Use a greedy approach to calculate the minimum rate by ensuring we distribute all items within the given time limit. Note that Greedy Algorithms are not explicitly covered by this course. We encourage you to follow your curiosity and research greedy algorithms on your own!
Plan the solution with appropriate visualizations and pseudocode.
-
Binary Search: Perform binary search over the possible distribution rates
r
, ranging from1
to the maximum number of items in any booth. -
Greedy Validation: For each candidate rate
r
, check if it is possible to distribute all items withinh
hours. If yes, try a smallerr
. If not, increaser
. -
Result: The smallest
r
that allows all items to be distributed withinh
hours is the answer.
Pseudocode:
1) Initialize `low` to 1 (smallest possible rate) and `high` to max(booths) (maximum possible rate).
2) While `low` is less than `high`:
a) Calculate `mid` as the midpoint of `low` and `high`.
b) Use a helper function `can_distribute_all_items_at_rate(mid)` to check if all items can be distributed within `h` hours at rate `mid`.
c) If true, set `high` to `mid` (try a smaller rate).
d) If false, set `low` to `mid + 1` (increase the rate).
3) Return `low` as the minimum valid distribution rate.
Pseudocode:
1) Initialize `hours_needed` to 0.
2) For each `items` in `booths`:
a) Calculate the number of hours needed to distribute `items` at rate `r` using `(items + r * 1) // r`.
b) Accumulate the hours in `hours_needed`.
3) Return `hours_needed <= h`.
Implement the code to solve the algorithm.
def min_distribution_rate(booths, h):
def can_distribute_all_items_at_rate(r):
hours_needed = 0
for items in booths:
hours_needed += (items + r * 1) // r # Equivalent to math.ceil(items / r)
return hours_needed <= h
# Binary search over r
low, high = 1, max(booths)
while low < high:
mid = (low + high) // 2
if can_distribute_all_items_at_rate(mid):
high = mid # Try to find a smaller valid r
else:
low = mid + 1 # Increase r because mid is too small
return low
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Trace through your code with the input
[3, 6, 7, 11]
andh = 8
:- The binary search should correctly identify 4 as the minimum distribution rate.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of booths.
-
Time Complexity:
O(N log M)
whereM
is the maximum number of items in any booth. This accounts for the binary search (log M
) and the linear pass through all booths (N
) for each midpoint calculation. -
Space Complexity:
O(1)
as the space used is constant regardless of input size.