Compute the minimum cost needed to reach every position in an array or sequence from a starting point under the problem's movement rules.
Minimum Cost To Reach Every Position
You are given a sequence of positions and a rule for moving between them with an associated cost. Starting from an initial position, determine the minimum cost required to reach every reachable position.
The exact movement constraints may vary by platform formulation, but the core task is the same: for each position, choose the cheapest valid way to arrive there from earlier reachable positions.
A typical solution uses dynamic programming or a shortest-path-like relaxation over the array, since each position's best cost depends on previously computed states.
Input Format
- The input describes the positions, movement options, and cost values.
- One position is designated as the start.
- Costs are non-negative unless otherwise stated by the platform.
Output Format
- Return or print the minimum cost for each position.
- If a position is unreachable, return the platform's designated sentinel value or equivalent representation.
Constraints
- The number of positions is typically up to in interview-style variants.
- Movement rules are usually local or forward-only, allowing linear or near-linear passes.
- Cost values fit in standard integer ranges unless otherwise specified.
Hints
- Track the best known cost for each position as you iterate.
- If movement only goes forward, an order-dependent DP often works.
- If multiple transitions can reach the same position, keep the minimum over all valid predecessors.
Example 1
Input
n = 5 start = 0 moveCost(i, j) = 2 if j = i + 1 else 5
Output
[0, 2, 4, 6, 8]
Explanation
The cheapest way to reach each next position is to move one step at a time, costing 2 per move.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.