Merge Operations for Minimum Travel Time
hard · 30.7% accepted · 69 likes · top 8%
array · dynamic programming · prefix sum
Description
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 + 1 so that its time becomes time[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.
Solution