We’re preparing your current view and syncing the latest data.
Given an array and a set of rules for moving between positions, each with an associated cost, compute the minimum cost required to reach every position from a starting point. Provide the result for all positions in the array as per the movement rules defined.
An array of integers representing the cost or weights associated with positions.
An array of integers where each element is the minimum cost to reach that position from the starting position.
Constraints on the size of the array and the values within it should be specified depending on the problem context.
Example 1
Input
costs = [1, 2, 3, 4]
Output
[0, 1, 3, 6]
Explanation
Starting at position 0 with cost 0, moving to position 1 costs 1, to position 2 costs 3 (1+2), and to position 3 costs 6 (1+2+3). The minimum costs to reach each position are [0, 1, 3, 6].