Pier Ticket Window Time
Each ticket window takes a fixed number of minutes to serve one guest. The array rates stores those times, and guests tells you how many people must be served in total.
Your task is to find the minimum number of minutes needed so that all windows together can serve at least that many guests. A window with rate 2 can serve one guest every 2 minutes, so after 8 minutes it can handle 8 / 2 = 4 guests.
For example, if rates = [2,3] and guests = 6, the answer is 8. In 8 minutes, the first window serves 4 guests and the second serves 2, which reaches the target of 6. If guests = 0, the answer is 0 because no waiting time is needed at all.
So the problem is to find the earliest minute where the total number of served guests becomes large enough.
Example Input & Output
In 8 minutes, window 1 serves 4 guests and window 2 serves 2 guests.
With no guests, zero minutes are required.
After 20 minutes the windows collectively handle eight guests.
Algorithm Flow

Solution Approach
This is a binary search on time. Instead of trying every minute one by one, we ask a yes-or-no question: can all windows serve at least guests people within mid minutes?
To check one time value, add up how many guests each window can finish:
If served >= guests, then mid minutes is enough, so we try to find a smaller valid time. If not, we need more time and move the search to the right.
A safe search range is from 0 to minRate * guests, because the fastest window could serve everyone alone in that many minutes. Then we binary search for the smallest valid time. When the search ends, that left boundary is the answer.
This works in O(n log answer) time, where n is the number of windows, and it uses O(1) extra space.
Best Answers
import java.util.*;
class Solution {
public int pier_ticket_window_time(Object input) {
Object[] args = (Object[]) input;
int[] rates = (int[]) args[0];
int guests = (int) args[1];
if (guests == 0) return 0;
long minRate = rates[0];
for (int r : rates) if (r < minRate) minRate = r;
long low = 1, high = minRate * guests, ans = high;
while (low <= high) {
long mid = low + (high - low) / 2;
long total = 0;
for (int r : rates) total += mid / r;
if (total >= guests) { ans = mid; high = mid - 1; }
else low = mid + 1;
}
return (int) ans;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
