Merge Operations for Minimum Travel Time Problem
Description
LeetCode Problem 3538.
You are given a straight road of length l km, an integer n, an integer k, and two integer arrays, position and time, each of length n.
The array position lists the positions (in km) of signs in strictly increasing order (with position[0] = 0 and position[n - 1] = l).
Each time[i] represents the time (in minutes) required to travel 1 km between position[i] and position[i + 1].
You must perform exactly k merge operations. In one merge, you can choose any two adjacent signs at indices i and i + 1 (with i > 0 and i + 1 < n) and:
- Update the sign at index
i + 1so that its time becomestime[i] + time[i + 1]. - Remove the sign at index
i.
Return the minimum total travel time (in minutes) to travel from 0 to l after exactly k merges.
Example 1:
Input: l = 10, n = 4, k = 1, position = [0,3,8,10], time = [5,8,3,6]
Output: 62
Explanation:
- Merge the signs at indices 1 and 2. Remove the sign at index 1, and change the time at index 2 to 8 + 3 = 11.
- After the merge:
- position array: [0, 8, 10]
- time array: [5, 11, 6]
*
- Segment Distance (km) Time per km (min) Segment Travel Time (min)
0 → 8 8 5 8 × 5 = 40
8 → 10 2 11 2 × 11 = 22
- Total Travel Time: 40 + 22 = 62, which is the minimum possible time after exactly 1 merge.
Example 2:
Input: l = 5, n = 5, k = 1, position = [0,1,2,3,5], time = [8,3,9,3,3]
Output: 34
Explanation:
- Merge the signs at indices 1 and 2. Remove the sign at index 1, and change the time at index 2 to 3 + 9 = 12.
- After the merge:
- position array: [0, 2, 3, 5]
- time array: [8, 12, 3, 3]
*
- Segment Distance (km) Time per km (min) Segment Travel Time (min)
0 → 2 2 8 2 × 8 = 16
2 → 3 1 12 1 × 12 = 12
3 → 5 2 3 2 × 3 = 6
- Total Travel Time: 16 + 12 + 6 = 34, which is the minimum possible time after exactly 1 merge.
Constraints:
1 <= l <= 10^52 <= n <= min(l + 1, 50)0 <= k <= min(n - 2, 10)position.length == nposition[0] = 0andposition[n - 1] = lpositionis sorted in strictly increasing order.time.length == n1 <= time[i] <= 1001 <= sum(time) <= 100
Solution
Approach: Dynamic Programming
After the merges, every kept sign carries the sum of the times of itself and the signs removed just before it, and a kept sign a followed by the next kept sign b costs (position[b] - position[a]) times that sum. Let f[a][j][p] be the minimum cost to reach kept sign a after removing j signs, where p marks the previous kept sign. A prefix sum of time gives the speed of a from p. From a try every next kept sign b, which removes b - a - 1 signs and must keep the total within k. The answer is the minimum of f[n - 1][k][*].
Complexity: O(n^3 * k) time and O(n^2 * k) extra space, where n is the number of signs.
class Solution:
def minTravelTime(self, l: int, n: int, k: int, position: List[int], time: List[int]) -> int:
inf = float("inf")
pre = [0] * (n + 1)
for i, t in enumerate(time):
pre[i + 1] = pre[i] + t
# f[a][j][pv]: kept sign a, j signs removed so far, previous kept sign is pv - 1
f = [[[inf] * (n + 1) for _ in range(k + 1)] for _ in range(n)]
f[0][0][0] = 0
for a in range(n - 1):
for j in range(k + 1):
for pv in range(a + 1):
cur = f[a][j][pv]
if cur == inf:
continue
speed = pre[a + 1] - pre[pv]
for b in range(a + 1, n):
nj = j + b - a - 1
if nj > k:
break
cost = cur + (position[b] - position[a]) * speed
if cost < f[b][nj][a + 1]:
f[b][nj][a + 1] = cost
return min(f[n - 1][k])