Maximizing Minimum Customer Satisfaction with Delivery Slots
Swiggy strives for exceptional customer satisfaction, especially during peak hours. A crucial aspect is assigning orders to available delivery slots such that customers are as happy as possible. Each order has a preferred delivery time and a flexibility window. We define customer satisfaction as 100 - |actualDeliveryTime - preferredDeliveryTime|, ensuring it's non-negative. We have a set of pending orders and a set of fixed delivery slots. Our goal is to assign as many orders as possible (up to the minimum of available orders and slots) to distinct slots, maximizing the minimum satisfaction score achieved across all assigned orders.
Implement a function maxMinSatisfaction(orders, slots) that determines the highest possible minimum satisfaction score that can be achieved for a set of assigned orders.
Your function should:
orders, where each order is [preferredTime, flexibility].slots, where each element is a slotTime.min(number of orders, number of slots) orders to distinct slots.Function Signature:
function maxMinSatisfaction(orders: number[][], slots: number[]): number
Parameters:
orders: An array of arrays, where orders[i] = [preferredTime_i, flexibility_i].
preferredTime_i: An integer representing the customer's ideal delivery time for order i.flexibility_i: An integer representing the maximum acceptable deviation from preferredTime_i. An order can only be assigned to a slot s if abs(s - preferredTime_i) <= flexibility_i. This is a hard constraint.slots: An array of integers, where slots[j] is the fixed time for the j-th available delivery slot.Return Value:
0.actualTime is calculated as max(0, 100 - abs(actualTime - preferredTime)). This score must be non-negative, meaning abs(actualTime - preferredTime) cannot exceed 100 for any positive satisfaction.i can be assigned to a slot s if and only if abs(s - preferredTime_i) <= flexibility_i AND abs(s - preferredTime_i) <= (100 - S_min_target) (where S_min_target is the minimum satisfaction we are trying to achieve).orders or slots are empty, or no assignments are possible, the minimum satisfaction is 0.Examples
Example 1
Input:
orders = [[10, 5], [20, 5]], slots = [12, 18]
Output:
98
Note:
We need to assign min(2, 2) = 2 orders. Let's binary search for the minimum satisfaction S_min.
If S_min = 98, the allowed deviation for satisfaction is 100 - 98 = 2.
preferredTime=10, flexibility=5. Effective deviation for assignment is min(5, 2) = 2. So, valid slot range is [10-2, 10+2] = [8, 12].preferredTime=20, flexibility=5. Effective deviation for assignment is min(5, 2) = 2. So, valid slot range is [20-2, 20+2] = [18, 22].
Available orders for checking: [[8, 12], [18, 22]]. Available slots: [12, 18].
Using a greedy approach:O1:[8,12], O2:[18,22].[12, 18].pq (stores end times of available orders) and assigned_count = 0.
12: Add O1 (end=12) to pq. pq=[12]. Pop 12 from pq (as 12 >= 12). assigned_count = 1.18: Add O2 (end=22) to pq. pq=[22]. Pop 22 from pq (as 22 >= 18). assigned_count = 2.
Since assigned_count = 2, which is >= min(2,2), S_min=98 is achievable.
If we try S_min = 99, the allowed deviation for satisfaction is 100 - 99 = 1.min(5, 1) = 1. Range [9, 11].min(5, 1) = 1. Range [19, 21].
Neither slot 12 nor 18 falls into these ranges. So S_min=99 is not achievable.
Thus, the maximum minimum satisfaction is 98.Example 2
Input:
orders = [[50, 10]], slots = [50]
Output:
100
Note:
We need to assign min(1, 1) = 1 order.
If S_min = 100, the allowed deviation for satisfaction is 100 - 100 = 0.
preferredTime=50, flexibility=10. Effective deviation is min(10, 0) = 0. Valid slot range: [50-0, 50+0] = [50, 50].
Available orders: [[50, 50]]. Available slots: [50].
Using greedy:
50: Add O1 (end=50) to pq. pq=[50]. Pop 50 from pq. assigned_count = 1.
Since assigned_count = 1, which is >= min(1,1), S_min=100 is achievable.
This is the maximum possible satisfaction, so the answer is 100.Example 3
Input:
orders = [[10, 2], [30, 2]], slots = [11, 29]
Output:
99
Note:
We need to assign min(2, 2) = 2 orders.
If S_min = 99, the allowed deviation for satisfaction is 100 - 99 = 1.
preferredTime=10, flexibility=2. Effective deviation is min(2, 1) = 1. Valid slot range: [10-1, 10+1] = [9, 11].preferredTime=30, flexibility=2. Effective deviation is min(2, 1) = 1. Valid slot range: [30-1, 30+1] = [29, 31].
Available orders: [[9, 11], [29, 31]]. Available slots: [11, 29].
Using greedy:
11: Add O1 (end=11) to pq. pq=[11]. Pop 11 from pq. assigned_count = 1.29: Add O2 (end=31) to pq. pq=[31]. Pop 31 from pq. assigned_count = 2.
Since assigned_count = 2, which is >= min(2,2), S_min=99 is achievable.
If we try S_min = 100, the allowed deviation for satisfaction is 0.min(2, 0) = 0. Range [10, 10].min(2, 0) = 0. Range [30, 30].
Neither slot 11 nor 29 falls into these very narrow ranges. So S_min=100 is not achievable.
Thus, the maximum minimum satisfaction is 99.Constraints
1 <= orders.length, slots.length <= 10^50 <= preferredTime_i, flexibility_i, slotTime_j <= 10^9preferredTime, slotTime) and flexibility are non-negative integers.0 and 100, inclusive.10^5) within typical time limits (e.g., O(log(S_range) * (N log N + M log M)) overall).Loading editor...