Unit 7 Session 2 (Click for link to problem statements)
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?
h
is less than the number of booths?
r
be fractional?
r
must be an integer as it represents the number of items distributed per hour.r
, even if more time is available.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:
r
.Plan the solution with appropriate visualizations and pseudocode.
1) Binary Search: Perform binary search over the possible distribution rates r
, ranging from 1
to the maximum number of items in any booth.
2) Greedy Validation: For each candidate rate r
, check if it is possible to distribute all items within h
hours. If yes, try a smaller r
. If not, increase r
.
3) Result: The smallest r
that allows all items to be distributed within h
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.
can_distribute_all_items_at_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.
[3, 6, 7, 11]
and h = 8
:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of booths.
O(N log M)
where M
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.O(1)
as the space used is constant regardless of input size.