BudiBadu Logo

Pier Ticket Window Time

Binary Search Easy 0 views
Like11

Pier Ticket Window Time asks for the minimum time required for multiple ticket windows to process a target number of guests. Each window has a fixed processing time per guest and works in parallel. Given the list of window times and required guests, return the smallest integer minute value where total processed guests is at least the target.

The efficient strategy is binary search on time, not minute-by-minute simulation. For a candidate time T, each window contributes T // rate processed guests, and the total feasibility check is the sum across all windows. If feasible, search left for a smaller valid time; otherwise search right. This produces fast performance even when guest targets are large and window rates vary significantly.

Judge checks include uniform windows, mixed-speed windows, single-window scenarios, and large targets where precision in boundary updates matters. Return only the minimum completion time as an integer. The critical detail is monotonicity: once a time value can process enough guests, any larger time can too. That monotonic property is what makes binary search correct and keeps the implementation both concise and scalable.

Examples

Example 1
Input
rates = [2,3], guests = 6
Output
8
Explanation

In 8 minutes, window 1 serves 4 guests and window 2 serves 2 guests.

Example 2
Input
rates = [4], guests = 0
Output
0
Explanation

With no guests, zero minutes are required.

Example 3
Input
rates = [5,7,10], guests = 8
Output
20
Explanation

After 20 minutes the windows collectively handle eight guests.

Algorithm Flow

Recommendation Algorithm Flow for Pier Ticket Window Time - Budibadu
Recommendation Algorithm Flow for Pier Ticket Window Time - Budibadu

Best Answers

java
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;
    }
}