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 stopi-1to stopi - 🚄 Express Route:
express[i]= cost from stopi-1to stopi - 💰 Transfer Cost: Pay
expressCosteach 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code