← 返回 atlassian 的题目列表Karat Carpool on Linear Routes
类型:qbank
Given directed linear roads, two car starting locations, and people living at locations on the route to a campground, assign each person to the car that reaches them first. Ties can go to either car.
Requirements
Roads are directed edges of the form [Origin, Destination, Duration].
Routes are linear: each location leads to at most one next location, with no loops or detours.
Both cars leave from their starting locations at the same time.
The first car to pass a person's location picks that person up.
If both cars arrive at the same time, the person can go in either car.
Return the people assigned to each car when arriving at the campground.
Examples
roads1 = [
['Bridgewater', 'Caledonia', '30'],
['Caledonia', 'New Grafton', '15'],
['New Grafton', 'Campground', '5'],
['Milton', 'New Grafton', '30'],
['Liverpool', 'Milton', '10']
]
starts1 = ['Bridgewater', 'Liverpool']
people1 = [
['Jessie', 'Bridgewater'], ['Travis', 'Caledonia'],
['Jeremy', 'New Grafton'], ['Katie', 'Liverpool']
]
Output: [Jessie, Travis], [Katie, Jeremy]
roads2 = [['Riverport', 'Chester', '40'], ['Chester', 'Campground', '60'], ['Halifax', 'Chester', '40']]
starts2 = ['Riverport', 'Halifax']
people2 = [['Colin', 'Riverport'], ['Sam', 'Chester'], ['Alyssa', 'Halifax']]
Output: [Colin, Sam], [Alyssa] OR [Colin], [Alyssa, Sam]
Notes
The source prompt defines n as the number of roads.
Build arrival-time maps from each starting location, then compare times for every person's location.
Preparation
Implement path traversal from each start using an adjacency map.
Test tie cases, people at starting locations, people at merge points, and reversed order of starting locations.