Imagine you're developing a core feature for a ride-sharing service, similar to Uber Pool, where the goal is to efficiently group individual ride requests into compatible batches. The objective is to minimize the total number of vehicles required by maximizing the number of compatible riders grouped into each vehicle, while respecting batch size limits.
Each incoming ride request provides essential details. Your task is to identify and form groups (batches) of rides, with a maximum of 3 rides per batch, that can be served together based on compatibility criteria.
Ride Data Format:
Each ride is represented as a list of three strings:
{"ride_id", "pickup_location", "pickup_time"}
Where:
ride_id : A unique identifier for the ride (e.g., "123", "456"). This will always be a string representing a positive integer.pickup_location : The specific location where the rider needs to be picked up (e.g., "A", "Downtown Station", "123 Main St"). This will always be a string.pickup_time : The scheduled pickup time in a 24-hour "HH:MM" format (e.g., "09:30", "14:15").Compatibility Rules:
Two or more rides are considered compatible and can be grouped into the same batch if, and only if:
Batching Constraints:
Output Ordering:
The final list of batches should be ordered based on the earliest original input index of any ride within that batch. For example, if a batch contains rides that were originally at index 0 and index 5 in the input list, and another batch contains rides from index 1 and index 2, the batch containing the ride from index 0 would appear first.
Note: The batches are ordered by the earliest input index: [1,2] (index 0), [3,5] (index 2), [4] (index 3), [6] (index 5).
Your Task:Implement a function that takes the list of ride requests as input and returns the final list of batches according to the rules and output ordering specified above.
rides = [
["1, Downtown Station, 09:00"],
["2, Downtown Station, 09:05"],
["3, Uptown Plaza, 10:00"],
["4, Downtown Station, 09:12"],
["5, Uptown Plaza, 10:07"],
["6, Airport Terminal, 11:00"]
]
return = [[1, 2],[3, 5],[4],[6]]- The maximum number of rides (N) up to 10000.
- (execution time limit) 0.5 seconds (cpp)
- Jump Game with Prime-3 StepsOA · Seen Jun 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · Seen May 2026
- Tournament Rounds by RankPHONE SCREEN · Seen May 2026
- Farthest Seat AssignmentONSITE INTERVIEW · Seen May 2026
- Convex Function MinimizationPHONE SCREEN · Seen May 2026
- Maximal Square AreaONSITE INTERVIEW · Seen May 2026
- First Unique IP Hitting the ServerPHONE SCREEN · Seen May 2026
public int[][] batchRides(String[][] rides) {
// write your code here
}