Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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:
i + 1 so that its time becomes time[i] + time[i + 1].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.
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 |
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:
3 + 9 = 12.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 |
16 + 12 + 6 = 34, which is the minimum possible time after exactly 1 merge.Constraints:
1 <= l <= 1052 <= n <= min(l + 1, 50)0 <= k <= min(n - 2, 10)position.length == nposition[0] = 0 and position[n - 1] = lposition is sorted in strictly increasing order.time.length == n1 <= time[i] <= 1001 <= sum(time) <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
10 4 1 [0,3,8,10] [5,8,3,6]
5 5 1 [0,1,2,3,5] [8,3,9,3,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3538: Merge Operations for Minimum Travel Time
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3538: Merge Operations for Minimum Travel Time
// package main
//
// import "math"
//
// // https://space.bilibili.com/206214
// func minTravelTime1(_, n, K int, position, time []int) int {
// s := make([]int, n)
// for i, t := range time[:n-1] { // time[n-1] 用不到
// s[i+1] = s[i] + t // 计算 time 的前缀和
// }
//
// memo := make([][][]int, n-1)
// for i := range memo {
// memo[i] = make([][]int, K+1)
// for j := range memo[i] {
// memo[i][j] = make([]int, K+1)
// }
// }
// var dfs func(int, int, int) int
// dfs = func(j, sz, leftK int) int {
// if j == n-1 { // 到达终点
// if leftK > 0 { // 不合法
// return math.MaxInt / 2 // 避免下面计算 r 的地方加法溢出
// }
// return 0
// }
// p := &memo[j][sz][leftK]
// if *p > 0 {
// return *p
// }
// res := math.MaxInt
// t := s[j+1] - s[j-sz] // 合并到 time[j] 的时间
// // 枚举下一个子数组 [j+1, k]
// for k := j + 1; k < min(n, j+2+leftK); k++ {
// r := dfs(k, k-j-1, leftK-(k-j-1)) + (position[k]-position[j])*t
// res = min(res, r)
// }
// *p = res
// return res
// }
// return dfs(0, 0, K) // 第一个子数组是 [0, 0]
// }
//
// func minTravelTime(_, n, K int, position, time []int) int {
// s := make([]int, n)
// for i, t := range time[:n-1] { // time[n-1] 用不到
// s[i+1] = s[i] + t // 计算 time 的前缀和
// }
//
// f := make([][][]int, n)
// for j := range f {
// f[j] = make([][]int, K+1)
// for sz := range f[j] {
// f[j][sz] = make([]int, K+1)
// for leftK := range f[j][sz] {
// f[j][sz][leftK] = math.MaxInt / 2
// }
// }
// }
// for sz := range K + 1 {
// f[n-1][sz][0] = 0
// }
//
// for j := n - 2; j >= 0; j-- { // 转移来源 k 比 j 大,所以要倒序
// for sz := range min(K, j) + 1 {
// t := s[j+1] - s[j-sz] // 合并到 time[j] 的时间
// for leftK := range min(K, n-2-j) + 1 {
// res := math.MaxInt
// // 枚举下一个子数组 [j+1, k]
// for k := j + 1; k <= j+1+leftK; k++ {
// r := f[k][k-j-1][leftK-(k-j-1)] + (position[k]-position[j])*t
// res = min(res, r)
// }
// f[j][sz][leftK] = res
// }
// }
// }
// return f[0][0][K] // 第一个子数组是 [0, 0]
// }
// Accepted solution for LeetCode #3538: Merge Operations for Minimum Travel Time
package main
import "math"
// https://space.bilibili.com/206214
func minTravelTime1(_, n, K int, position, time []int) int {
s := make([]int, n)
for i, t := range time[:n-1] { // time[n-1] 用不到
s[i+1] = s[i] + t // 计算 time 的前缀和
}
memo := make([][][]int, n-1)
for i := range memo {
memo[i] = make([][]int, K+1)
for j := range memo[i] {
memo[i][j] = make([]int, K+1)
}
}
var dfs func(int, int, int) int
dfs = func(j, sz, leftK int) int {
if j == n-1 { // 到达终点
if leftK > 0 { // 不合法
return math.MaxInt / 2 // 避免下面计算 r 的地方加法溢出
}
return 0
}
p := &memo[j][sz][leftK]
if *p > 0 {
return *p
}
res := math.MaxInt
t := s[j+1] - s[j-sz] // 合并到 time[j] 的时间
// 枚举下一个子数组 [j+1, k]
for k := j + 1; k < min(n, j+2+leftK); k++ {
r := dfs(k, k-j-1, leftK-(k-j-1)) + (position[k]-position[j])*t
res = min(res, r)
}
*p = res
return res
}
return dfs(0, 0, K) // 第一个子数组是 [0, 0]
}
func minTravelTime(_, n, K int, position, time []int) int {
s := make([]int, n)
for i, t := range time[:n-1] { // time[n-1] 用不到
s[i+1] = s[i] + t // 计算 time 的前缀和
}
f := make([][][]int, n)
for j := range f {
f[j] = make([][]int, K+1)
for sz := range f[j] {
f[j][sz] = make([]int, K+1)
for leftK := range f[j][sz] {
f[j][sz][leftK] = math.MaxInt / 2
}
}
}
for sz := range K + 1 {
f[n-1][sz][0] = 0
}
for j := n - 2; j >= 0; j-- { // 转移来源 k 比 j 大,所以要倒序
for sz := range min(K, j) + 1 {
t := s[j+1] - s[j-sz] // 合并到 time[j] 的时间
for leftK := range min(K, n-2-j) + 1 {
res := math.MaxInt
// 枚举下一个子数组 [j+1, k]
for k := j + 1; k <= j+1+leftK; k++ {
r := f[k][k-j-1][leftK-(k-j-1)] + (position[k]-position[j])*t
res = min(res, r)
}
f[j][sz][leftK] = res
}
}
}
return f[0][0][K] // 第一个子数组是 [0, 0]
}
# Accepted solution for LeetCode #3538: Merge Operations for Minimum Travel Time
class Solution:
def minTravelTime(
self,
l: int,
n: int,
k: int,
position: list[int],
time: list[int]
) -> int:
prefix = list(itertools.accumulate(time))
@functools.lru_cache(None)
def dp(i: int, skips: int, last: int) -> int:
"""
Returns the minimum travel time to reach the last stop from i-th stop,
with `skips` skips remaining, and the last stop being `last`.
"""
if i == n - 1:
return 0 if skips == 0 else math.inf
res = math.inf
rate = prefix[i] - (prefix[last - 1] if last > 0 else 0)
end = min(n - 1, i + skips + 1)
for j in range(i + 1, end + 1):
distance = position[j] - position[i]
res = min(res, distance * rate + dp(j, skips - (j - i - 1), i + 1))
return res
return dp(0, k, 0)
// Accepted solution for LeetCode #3538: Merge Operations for Minimum Travel Time
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3538: Merge Operations for Minimum Travel Time
// package main
//
// import "math"
//
// // https://space.bilibili.com/206214
// func minTravelTime1(_, n, K int, position, time []int) int {
// s := make([]int, n)
// for i, t := range time[:n-1] { // time[n-1] 用不到
// s[i+1] = s[i] + t // 计算 time 的前缀和
// }
//
// memo := make([][][]int, n-1)
// for i := range memo {
// memo[i] = make([][]int, K+1)
// for j := range memo[i] {
// memo[i][j] = make([]int, K+1)
// }
// }
// var dfs func(int, int, int) int
// dfs = func(j, sz, leftK int) int {
// if j == n-1 { // 到达终点
// if leftK > 0 { // 不合法
// return math.MaxInt / 2 // 避免下面计算 r 的地方加法溢出
// }
// return 0
// }
// p := &memo[j][sz][leftK]
// if *p > 0 {
// return *p
// }
// res := math.MaxInt
// t := s[j+1] - s[j-sz] // 合并到 time[j] 的时间
// // 枚举下一个子数组 [j+1, k]
// for k := j + 1; k < min(n, j+2+leftK); k++ {
// r := dfs(k, k-j-1, leftK-(k-j-1)) + (position[k]-position[j])*t
// res = min(res, r)
// }
// *p = res
// return res
// }
// return dfs(0, 0, K) // 第一个子数组是 [0, 0]
// }
//
// func minTravelTime(_, n, K int, position, time []int) int {
// s := make([]int, n)
// for i, t := range time[:n-1] { // time[n-1] 用不到
// s[i+1] = s[i] + t // 计算 time 的前缀和
// }
//
// f := make([][][]int, n)
// for j := range f {
// f[j] = make([][]int, K+1)
// for sz := range f[j] {
// f[j][sz] = make([]int, K+1)
// for leftK := range f[j][sz] {
// f[j][sz][leftK] = math.MaxInt / 2
// }
// }
// }
// for sz := range K + 1 {
// f[n-1][sz][0] = 0
// }
//
// for j := n - 2; j >= 0; j-- { // 转移来源 k 比 j 大,所以要倒序
// for sz := range min(K, j) + 1 {
// t := s[j+1] - s[j-sz] // 合并到 time[j] 的时间
// for leftK := range min(K, n-2-j) + 1 {
// res := math.MaxInt
// // 枚举下一个子数组 [j+1, k]
// for k := j + 1; k <= j+1+leftK; k++ {
// r := f[k][k-j-1][leftK-(k-j-1)] + (position[k]-position[j])*t
// res = min(res, r)
// }
// f[j][sz][leftK] = res
// }
// }
// }
// return f[0][0][K] // 第一个子数组是 [0, 0]
// }
// Accepted solution for LeetCode #3538: Merge Operations for Minimum Travel Time
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3538: Merge Operations for Minimum Travel Time
// package main
//
// import "math"
//
// // https://space.bilibili.com/206214
// func minTravelTime1(_, n, K int, position, time []int) int {
// s := make([]int, n)
// for i, t := range time[:n-1] { // time[n-1] 用不到
// s[i+1] = s[i] + t // 计算 time 的前缀和
// }
//
// memo := make([][][]int, n-1)
// for i := range memo {
// memo[i] = make([][]int, K+1)
// for j := range memo[i] {
// memo[i][j] = make([]int, K+1)
// }
// }
// var dfs func(int, int, int) int
// dfs = func(j, sz, leftK int) int {
// if j == n-1 { // 到达终点
// if leftK > 0 { // 不合法
// return math.MaxInt / 2 // 避免下面计算 r 的地方加法溢出
// }
// return 0
// }
// p := &memo[j][sz][leftK]
// if *p > 0 {
// return *p
// }
// res := math.MaxInt
// t := s[j+1] - s[j-sz] // 合并到 time[j] 的时间
// // 枚举下一个子数组 [j+1, k]
// for k := j + 1; k < min(n, j+2+leftK); k++ {
// r := dfs(k, k-j-1, leftK-(k-j-1)) + (position[k]-position[j])*t
// res = min(res, r)
// }
// *p = res
// return res
// }
// return dfs(0, 0, K) // 第一个子数组是 [0, 0]
// }
//
// func minTravelTime(_, n, K int, position, time []int) int {
// s := make([]int, n)
// for i, t := range time[:n-1] { // time[n-1] 用不到
// s[i+1] = s[i] + t // 计算 time 的前缀和
// }
//
// f := make([][][]int, n)
// for j := range f {
// f[j] = make([][]int, K+1)
// for sz := range f[j] {
// f[j][sz] = make([]int, K+1)
// for leftK := range f[j][sz] {
// f[j][sz][leftK] = math.MaxInt / 2
// }
// }
// }
// for sz := range K + 1 {
// f[n-1][sz][0] = 0
// }
//
// for j := n - 2; j >= 0; j-- { // 转移来源 k 比 j 大,所以要倒序
// for sz := range min(K, j) + 1 {
// t := s[j+1] - s[j-sz] // 合并到 time[j] 的时间
// for leftK := range min(K, n-2-j) + 1 {
// res := math.MaxInt
// // 枚举下一个子数组 [j+1, k]
// for k := j + 1; k <= j+1+leftK; k++ {
// r := f[k][k-j-1][leftK-(k-j-1)] + (position[k]-position[j])*t
// res = min(res, r)
// }
// f[j][sz][leftK] = res
// }
// }
// }
// return f[0][0][K] // 第一个子数组是 [0, 0]
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.