Problem · Array

Allocate Customers to Preferred Sites

Learn this problem
MediumYelp logoYelpFULLTIMEONSITE INTERVIEW

Problem statement

There are preferredSite.length customers and capacity.length sites. Customer i requests exactly one site, preferredSite[i], and has integer priority priority[i]. Site s can accept at most capacity[s] customers.

For each site independently, accept the requesting customers with the highest priorities until its capacity is full. When priorities tie, accept the lower customer index first.

Return an array in customer-index order. The value at index i is the requested site when customer i is accepted, or -1 otherwise.

Function

allocatePreferredSites(preferredSite: int[], priority: int[], capacity: int[]) → int[]

Examples

Example 1

preferredSite = [0,0,1,0]priority = [5,9,4,9]capacity = [2,1]return = [-1,0,1,0]

Site 0 accepts customers 1 and 3, while site 1 accepts customer 2. Customer 0 is rejected.

Example 2

preferredSite = [1,1,1]priority = [3,3,5]capacity = [0,1]return = [-1,-1,1]

The only slot at site 1 goes to customer 2 because priority 5 is highest.

Constraints

  • 1 <= preferredSite.length = priority.length <= 200000.
  • 1 <= capacity.length <= 200000.
  • 0 <= preferredSite[i] < capacity.length.
  • 0 <= priority[i] <= 10^9.
  • 0 <= capacity[s] <= preferredSite.length.

More Yelp problems

drafts saved locally
public int[] allocatePreferredSites(int[] preferredSite, int[] priority, int[] capacity) {
    // Write your code here.
}
preferredSite[0,0,1,0]
priority[5,9,4,9]
capacity[2,1]
expected[-1,0,1,0]
checking account