Minimum Costs Using the Train Line - Problem

🚆 Minimum Costs Using the Train Line

Imagine you're planning a journey through a city with two train routes: a regular route and an express route. Both routes connect the same n + 1 stops (labeled 0 to n), but they have different costs and speeds.

Your Journey: You start at stop 0 on the regular route and want to find the cheapest way to reach each subsequent stop.

Route Rules:

  • 🚂 Regular Route: regular[i] = cost from stop i-1 to stop i
  • 🚄 Express Route: express[i] = cost from stop i-1 to stop i
  • 💰 Transfer Cost: Pay expressCost each time you switch from regular to express
  • 🆓 Free Transfer: Switch from express back to regular anytime (no cost)

Goal: Return an array costs where costs[i] is the minimum cost to reach stop i from stop 0.

Example: If regular = [1, 3, 2], express = [4, 1, 1], and expressCost = 2, you might take the regular route to stop 1 (cost: 1), then transfer to express for stops 2-3 (total additional cost: 2 + 1 + 1 = 4).

Input & Output

example_1.py — Basic Route Selection
$ Input: regular = [1, 3, 2], express = [4, 1, 1], expressCost = 2
Output: [1, 4, 5]
💡 Note: Stop 1: Take regular route (cost: 1). Stop 2: Stay on regular (cost: 1+3=4). Stop 3: Transfer to express at stop 1 is better: regular[0] + expressCost + express[1] + express[2] = 1 + 2 + 1 + 1 = 5, vs staying regular = 1+3+2 = 6.
example_2.py — Express Always Better
$ Input: regular = [10, 10, 10], express = [1, 1, 1], expressCost = 1
Output: [2, 3, 4]
💡 Note: Express route is much cheaper despite transfer cost. Stop 1: Transfer immediately (cost: 1+1=2). Stop 2: Continue on express (cost: 2+1=3). Stop 3: Continue on express (cost: 3+1=4).
example_3.py — High Transfer Cost
$ Input: regular = [2, 2, 2], express = [1, 1, 1], expressCost = 10
Output: [2, 4, 6]
💡 Note: Transfer cost is too high, so regular route is always better. Stop 1: Regular (cost: 2). Stop 2: Regular (cost: 4). Stop 3: Regular (cost: 6). Even though express is cheaper per segment, the 10-unit transfer cost makes it uneconomical.

Constraints

  • 1 ≤ n ≤ 1000
  • 1 ≤ regular[i], express[i] ≤ 1000
  • 1 ≤ expressCost ≤ 1000
  • Important: You always start on the regular route at stop 0

Visualization

Tap to expand
Minimum Costs Using the Train Line INPUT Two Train Routes (Stops 0 to 3) Express 0 1 2 3 4 1 1 Regular 0 1 2 3 1 3 2 +2 free Input Arrays: regular = [1, 3, 2] 1 3 2 express = [4, 1, 1] 4 1 1 expressCost = 2 2 ALGORITHM STEPS 1 Initialize reg_cost=0, exp_cost=2 (start at stop 0) 2 For each stop i Calculate costs via both regular and express routes 3 Update costs reg = min(reg+r[i], exp+r[i]) exp = min(reg+ec+e[i], exp+e[i]) 4 Store result costs[i] = min(reg, exp) DP Trace: Stop Reg Exp Min 1 1 6 1 2 4 4 4 3 6 5 5 FINAL RESULT Minimum costs to each stop: Stop 1 Cost: 1 (via Regular) Stop 2 Cost: 4 (Reg or Exp both=4) Stop 3 Cost: 5 (via Express) Output Array: 1 4 5 [1, 4, 5] OK - Verified Key Insight: This is a Dynamic Programming problem where we track two states at each stop: the minimum cost when on Regular route vs Express route. The asymmetric transfer cost (pay to go Express, free to return) creates optimal switching opportunities. Time: O(n), Space: O(1) TutorialsPoint - Minimum Costs Using the Train Line | Optimal DP Solution
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
43.7K Views
Medium Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen