Festival Peak Vendors

Hard
0/120
0 upvote
Asked in company
Google inc

Problem statement

A city festival has N food stalls. Each stall is defined by a start time, an end time, and a vendor type ('local' or 'guest'). A stall is considered "operating" at any time t if starttime <= t < endtime.


The city planner needs to allocate resources by determining the peak demand for each vendor type. Your task is to find two values:


1) The maximum number of local vendor stalls operating at the same time.


2) The maximum number of guest vendor stalls operating at the same time.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer N, the number of stalls.

The next N lines each contain two integers, start and end, and a string type ("local" or "guest"), separated by spaces.


Output Format:
Print a single line containing two space-separated integers: the maximum simultaneous count of local vendors followed by the maximum simultaneous count of guest vendors.


Note:
This problem can be efficiently solved by running a sweep-line algorithm twice, once for each vendor type.
Sample Input 1:
3
2 5 local
1 4 guest
3 7 local


Sample Output 1:
2 1


Explanation for Sample 1:
- Local Vendors: Intervals are `[2, 5)` and `[3, 7)`.
- At time 2, one local stall opens. (Active: 1)
- At time 3, another local stall opens. (Active: 2) This is the peak.
- At time 5, the first local stall closes. (Active: 1)
- At time 7, the second local stall closes. (Active: 0)
The maximum number of simultaneous local vendors is 2.
- Guest Vendors: Interval is `[1, 4)`. The maximum number of simultaneous guest vendors is 1.


Sample Input 2:
4
1 5 local
6 10 local
2 7 guest
8 12 guest


Sample Output 2:
1 1


Explanation for Sample 2:
- Local Vendors: Intervals `[1, 5)` and `[6, 10)` do not overlap. The maximum active at any time is 1.
- Guest Vendors:** Intervals `[2, 7)` and `[8, 12)` do not overlap. The maximum active at any time is 1.


Expected Time Complexity:
The expected time complexity is O(N log N).


Constraints:
1 <= N <= 10^5
0 <= start < end <= 10^9

Time limit: 1 sec
Approaches (1)
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Festival Peak Vendors
Full screen
Console