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.
There is a country of n cities numbered from 0 to n - 1 where all the cities are connected by bi-directional roads. The roads are represented as a 2D integer array edges where edges[i] = [xi, yi, timei] denotes a road between cities xi and yi that takes timei minutes to travel. There may be multiple roads of differing travel times connecting the same two cities, but no road connects a city to itself.
Each time you pass through a city, you must pay a passing fee. This is represented as a 0-indexed integer array passingFees of length n where passingFees[j] is the amount of dollars you must pay when you pass through city j.
In the beginning, you are at city 0 and want to reach city n - 1 in maxTime minutes or less. The cost of your journey is the summation of passing fees for each city that you passed through at some moment of your journey (including the source and destination cities).
Given maxTime, edges, and passingFees, return the minimum cost to complete your journey, or -1 if you cannot complete it within maxTime minutes.
Example 1:
Input: maxTime = 30, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3] Output: 11 Explanation: The path to take is 0 -> 1 -> 2 -> 5, which takes 30 minutes and has $11 worth of passing fees.
Example 2:
Input: maxTime = 29, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3] Output: 48 Explanation: The path to take is 0 -> 3 -> 4 -> 5, which takes 26 minutes and has $48 worth of passing fees. You cannot take path 0 -> 1 -> 2 -> 5 since it would take too long.
Example 3:
Input: maxTime = 25, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3] Output: -1 Explanation: There is no way to reach city 5 from city 0 within 25 minutes.
Constraints:
1 <= maxTime <= 1000n == passingFees.length2 <= n <= 1000n - 1 <= edges.length <= 10000 <= xi, yi <= n - 11 <= timei <= 10001 <= passingFees[j] <= 1000 Problem summary: There is a country of n cities numbered from 0 to n - 1 where all the cities are connected by bi-directional roads. The roads are represented as a 2D integer array edges where edges[i] = [xi, yi, timei] denotes a road between cities xi and yi that takes timei minutes to travel. There may be multiple roads of differing travel times connecting the same two cities, but no road connects a city to itself. Each time you pass through a city, you must pay a passing fee. This is represented as a 0-indexed integer array passingFees of length n where passingFees[j] is the amount of dollars you must pay when you pass through city j. In the beginning, you are at city 0 and want to reach city n - 1 in maxTime minutes or less. The cost of your journey is the summation of passing fees for each city that you passed through at some moment of your journey (including the source and destination cities).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
30 [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]] [5,1,2,20,20,3]
29 [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]] [5,1,2,20,20,3]
25 [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]] [5,1,2,20,20,3]
maximum-cost-of-trip-with-k-highways)maximum-path-quality-of-a-graph)minimum-cost-to-reach-city-with-discounts)find-minimum-time-to-reach-last-room-i)find-minimum-time-to-reach-last-room-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1928: Minimum Cost to Reach Destination in Time
class Solution {
public int minCost(int maxTime, int[][] edges, int[] passingFees) {
int m = maxTime, n = passingFees.length;
int[][] f = new int[m + 1][n];
final int inf = 1 << 30;
for (var g : f) {
Arrays.fill(g, inf);
}
f[0][0] = passingFees[0];
for (int i = 1; i <= m; ++i) {
for (var e : edges) {
int x = e[0], y = e[1], t = e[2];
if (t <= i) {
f[i][x] = Math.min(f[i][x], f[i - t][y] + passingFees[x]);
f[i][y] = Math.min(f[i][y], f[i - t][x] + passingFees[y]);
}
}
}
int ans = inf;
for (int i = 0; i <= m; ++i) {
ans = Math.min(ans, f[i][n - 1]);
}
return ans == inf ? -1 : ans;
}
}
// Accepted solution for LeetCode #1928: Minimum Cost to Reach Destination in Time
func minCost(maxTime int, edges [][]int, passingFees []int) int {
m, n := maxTime, len(passingFees)
f := make([][]int, m+1)
const inf int = 1 << 30
for i := range f {
f[i] = make([]int, n)
for j := range f[i] {
f[i][j] = inf
}
}
f[0][0] = passingFees[0]
for i := 1; i <= m; i++ {
for _, e := range edges {
x, y, t := e[0], e[1], e[2]
if t <= i {
f[i][x] = min(f[i][x], f[i-t][y]+passingFees[x])
f[i][y] = min(f[i][y], f[i-t][x]+passingFees[y])
}
}
}
ans := inf
for i := 1; i <= m; i++ {
ans = min(ans, f[i][n-1])
}
if ans == inf {
return -1
}
return ans
}
# Accepted solution for LeetCode #1928: Minimum Cost to Reach Destination in Time
class Solution:
def minCost(
self, maxTime: int, edges: List[List[int]], passingFees: List[int]
) -> int:
m, n = maxTime, len(passingFees)
f = [[inf] * n for _ in range(m + 1)]
f[0][0] = passingFees[0]
for i in range(1, m + 1):
for x, y, t in edges:
if t <= i:
f[i][x] = min(f[i][x], f[i - t][y] + passingFees[x])
f[i][y] = min(f[i][y], f[i - t][x] + passingFees[y])
ans = min(f[i][n - 1] for i in range(m + 1))
return ans if ans < inf else -1
// Accepted solution for LeetCode #1928: Minimum Cost to Reach Destination in Time
/**
* [1928] Minimum Cost to Reach Destination in Time
*
* There is a country of n cities numbered from 0 to n - 1 where all the cities are connected by bi-directional roads. The roads are represented as a 2D integer array edges where edges[i] = [xi, yi, timei] denotes a road between cities xi and yi that takes timei minutes to travel. There may be multiple roads of differing travel times connecting the same two cities, but no road connects a city to itself.
* Each time you pass through a city, you must pay a passing fee. This is represented as a 0-indexed integer array passingFees of length n where passingFees[j] is the amount of dollars you must pay when you pass through city j.
* In the beginning, you are at city 0 and want to reach city n - 1 in maxTime minutes or less. The cost of your journey is the summation of passing fees for each city that you passed through at some moment of your journey (including the source and destination cities).
* Given maxTime, edges, and passingFees, return the minimum cost to complete your journey, or -1 if you cannot complete it within maxTime minutes.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/06/04/leetgraph1-1.png" style="width: 371px; height: 171px;" />
*
* Input: maxTime = 30, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
* Output: 11
* Explanation: The path to take is 0 -> 1 -> 2 -> 5, which takes 30 minutes and has $11 worth of passing fees.
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/06/04/copy-of-leetgraph1-1.png" style="width: 371px; height: 171px;" />
*
* Input: maxTime = 29, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
* Output: 48
* Explanation: The path to take is 0 -> 3 -> 4 -> 5, which takes 26 minutes and has $48 worth of passing fees.
* You cannot take path 0 -> 1 -> 2 -> 5 since it would take too long.
*
* Example 3:
*
* Input: maxTime = 25, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
* Output: -1
* Explanation: There is no way to reach city 5 from city 0 within 25 minutes.
*
*
* Constraints:
*
* 1 <= maxTime <= 1000
* n == passingFees.length
* 2 <= n <= 1000
* n - 1 <= edges.length <= 1000
* 0 <= xi, yi <= n - 1
* 1 <= timei <= 1000
* 1 <= passingFees[j] <= 1000
* The graph may contain multiple edges between two nodes.
* The graph does not contain self loops.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-cost-to-reach-destination-in-time/
// discuss: https://leetcode.com/problems/minimum-cost-to-reach-destination-in-time/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn min_cost(max_time: i32, edges: Vec<Vec<i32>>, passing_fees: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1928_example_1() {
let max_time = 30;
let edges = vec![
vec![0, 1, 10],
vec![1, 2, 10],
vec![2, 5, 10],
vec![0, 3, 1],
vec![3, 4, 10],
vec![4, 5, 15],
];
let passing_fees = vec![5, 1, 2, 20, 20, 3];
let result = 11;
assert_eq!(Solution::min_cost(max_time, edges, passing_fees), result);
}
#[test]
#[ignore]
fn test_1928_example_2() {
let max_time = 29;
let edges = vec![
vec![0, 1, 10],
vec![1, 2, 10],
vec![2, 5, 10],
vec![0, 3, 1],
vec![3, 4, 10],
vec![4, 5, 15],
];
let passing_fees = vec![5, 1, 2, 20, 20, 3];
let result = 48;
assert_eq!(Solution::min_cost(max_time, edges, passing_fees), result);
}
#[test]
#[ignore]
fn test_1928_example_3() {
let max_time = 25;
let edges = vec![
vec![0, 1, 10],
vec![1, 2, 10],
vec![2, 5, 10],
vec![0, 3, 1],
vec![3, 4, 10],
vec![4, 5, 15],
];
let passing_fees = vec![5, 1, 2, 20, 20, 3];
let result = -1;
assert_eq!(Solution::min_cost(max_time, edges, passing_fees), result);
}
}
// Accepted solution for LeetCode #1928: Minimum Cost to Reach Destination in Time
function minCost(maxTime: number, edges: number[][], passingFees: number[]): number {
const [m, n] = [maxTime, passingFees.length];
const f: number[][] = Array.from({ length: m + 1 }, () => Array(n).fill(Infinity));
f[0][0] = passingFees[0];
for (let i = 1; i <= m; ++i) {
for (const [x, y, t] of edges) {
if (t <= i) {
f[i][x] = Math.min(f[i][x], f[i - t][y] + passingFees[x]);
f[i][y] = Math.min(f[i][y], f[i - t][x] + passingFees[y]);
}
}
}
let ans = Infinity;
for (let i = 1; i <= m; ++i) {
ans = Math.min(ans, f[i][n - 1]);
}
return ans === Infinity ? -1 : ans;
}
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.