A delivery rider serves m delivery zones connected by a circular road. The zones are numbered from 1 to m in clockwise order.
Each zone is adjacent to the zones immediately before and after it in circular order. Therefore, zone 1 is adjacent to zones 2 and m, and zone m is adjacent to zones m - 1 and 1.
The rider starts in zone 1 and receives a list of delivery stops. These stops must be visited in the given order.
Due to local roads and traffic, leaving zone i and reaching either adjacent zone takes zoneTimes.get(i - 1) minutes.
Return the minimum total travel time required to complete all deliveries.
DeliveryRoutePlanner
public long minimumTravelTime(List<Integer> zoneTimes, List<Integer> deliveryStops)
zoneTimes.get(i - 1) is the time required to leave zone i and reach either adjacent zone.deliveryStops contains the zones that must be visited, in order.1.0.1.m = zoneTimes.size()2 ≤ m ≤ 100,000q = deliveryStops.size()1 ≤ q ≤ 100,0000 ≤ i < m for zoneTimes.get(i)1 ≤ zoneTimes.get(i) ≤ 100,0000 ≤ i < q for deliveryStops.get(i)1 ≤ deliveryStops.get(i) ≤ mO(m + q) time.minimumTravelTime(zoneTimes = [4, 1, 7, 2, 5], deliveryStops = [4, 2, 2, 5])
Output: 23
The rider travels from zone 1 to zone 4 via zone 5, taking 4 + 5 = 9 minutes.
Traveling from zone 4 to zone 2 through zone 3 takes 2 + 7 = 9 minutes. The repeated stop in zone 2 requires no travel. Finally, traveling from zone 2 to zone 5 through zone 1 takes 1 + 4 = 5 minutes.
Therefore, the minimum total time is 9 + 9 + 0 + 5 = 23 minutes.
minimumTravelTime(zoneTimes = [8, 3, 6, 2, 7, 4], deliveryStops = [6, 3, 1])
Output: 30
The minimum travel times for the three trips are 8, 13, and 9 minutes.
minimumTravelTime(zoneTimes = [5, 2, 8], deliveryStops = [1, 1, 3, 3])
Output: 5
The first two deliveries are already in zone 1. The rider then travels directly from zone 1 to zone 3 in 5 minutes. The final delivery is also in zone 3.