|
| 1 | +3538\. Merge Operations for Minimum Travel Time |
| 2 | + |
| 3 | +Hard |
| 4 | + |
| 5 | +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`. |
| 6 | + |
| 7 | +The array `position` lists the positions (in km) of signs in **strictly** increasing order (with `position[0] = 0` and `position[n - 1] = l`). |
| 8 | + |
| 9 | +Each `time[i]` represents the time (in minutes) required to travel 1 km between `position[i]` and `position[i + 1]`. |
| 10 | + |
| 11 | +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: |
| 12 | + |
| 13 | +* Update the sign at index `i + 1` so that its time becomes `time[i] + time[i + 1]`. |
| 14 | +* Remove the sign at index `i`. |
| 15 | + |
| 16 | +Return the **minimum** **total** **travel time** (in minutes) to travel from 0 to `l` after **exactly** `k` merges. |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +**Input:** l = 10, n = 4, k = 1, position = [0,3,8,10], time = [5,8,3,6] |
| 21 | + |
| 22 | +**Output:** 62 |
| 23 | + |
| 24 | +**Explanation:** |
| 25 | + |
| 26 | +* 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`. |
| 27 | + |
| 28 | +* After the merge: |
| 29 | + * `position` array: `[0, 8, 10]` |
| 30 | + * `time` array: `[5, 11, 6]` |
| 31 | + |
| 32 | +| Segment | Distance (km) | Time per km (min) | Segment Travel Time (min) | |
| 33 | +|-----------|---------------|-------------------|----------------------------| |
| 34 | +| 0 → 8 | 8 | 5 | 8 × 5 = 40 | |
| 35 | +| 8 → 10 | 2 | 11 | 2 × 11 = 22 | |
| 36 | + |
| 37 | + |
| 38 | +* Total Travel Time: `40 + 22 = 62`, which is the minimum possible time after exactly 1 merge. |
| 39 | + |
| 40 | +**Example 2:** |
| 41 | + |
| 42 | +**Input:** l = 5, n = 5, k = 1, position = [0,1,2,3,5], time = [8,3,9,3,3] |
| 43 | + |
| 44 | +**Output:** 34 |
| 45 | + |
| 46 | +**Explanation:** |
| 47 | + |
| 48 | +* 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`. |
| 49 | +* After the merge: |
| 50 | + * `position` array: `[0, 2, 3, 5]` |
| 51 | + * `time` array: `[8, 12, 3, 3]` |
| 52 | + |
| 53 | +| Segment | Distance (km) | Time per km (min) | Segment Travel Time (min) | |
| 54 | +|-----------|---------------|-------------------|----------------------------| |
| 55 | +| 0 → 2 | 2 | 8 | 2 × 8 = 16 | |
| 56 | +| 2 → 3 | 1 | 12 | 1 × 12 = 12 | |
| 57 | +| 3 → 5 | 2 | 3 | 2 × 3 = 6 | |
| 58 | + |
| 59 | +* Total Travel Time: `16 + 12 + 6 = 34`**,** which is the minimum possible time after exactly 1 merge. |
| 60 | + |
| 61 | +**Constraints:** |
| 62 | + |
| 63 | +* <code>1 <= l <= 10<sup>5</sup></code> |
| 64 | +* `2 <= n <= min(l + 1, 50)` |
| 65 | +* `0 <= k <= min(n - 2, 10)` |
| 66 | +* `position.length == n` |
| 67 | +* `position[0] = 0` and `position[n - 1] = l` |
| 68 | +* `position` is sorted in strictly increasing order. |
| 69 | +* `time.length == n` |
| 70 | +* `1 <= time[i] <= 100` |
| 71 | +* `1 <= sum(time) <= 100` |
0 commit comments