2dbi
Home/Swiggy/Minimum Time for All Deliveries Given Agent Constraints
SSwiggy·DSASDE-1Online Assessment

Minimum Time for All Deliveries Given Agent Constraints

Problem

You have n delivery orders at positions on a number line and k delivery agents starting at position 0. Each agent moves at speed 1. Find the minimum time to complete all deliveries if agents work in parallel.

Example

orders = [2, 5, 9], k = 2
Output: 5  -- agent1: [2,5], agent2: [9] → max time = 5

Constraints

  • 1 ≤ k ≤ n ≤ 10^5
  • 1 ≤ orders[i] ≤ 10^9

Approach

Binary search on the answer. For a given time T, check if k agents can cover all orders greedily.

Follow-up

Agents can start from any depot location (not just 0). How does your approach change?

added 6 days ago
LeadersAccount