You are given an integer array cost of size n. You are currently at position n (at the end of the line) in a line of n + 1 people (numbered from 0 to n).
You wish to move forward in the line, but each person in front of you charges a specific amount to swap places. The cost to swap with person i is given by cost[i].
You are allowed to swap places with people as follows:
Return an array answer of size n, where answer[i] is the minimum total cost to reach each position i in the line.
Examples
Example 1
Input: cost = [5,3,4,1,3,2]
Output: [5,3,3,1,1,1]
Explanation:
We can get to each position in the following way:
Example 2
Input: cost = [1,2,4,6,7]
Output: [1,1,1,1,1]
Explanation:
We can swap with person 0 for a cost of 1, then we will be able to reach any position i for free.
Constraints
1 <= n == cost.length <= 100
1 <= cost[i] <= 100
3502. Minimum Cost to Reach Every Position
Easy
10 Points
Array
You are given an integer array cost of size n. You are currently at position n (at the end of the line) in a line of n + 1 people (numbered from 0 to n).
You wish to move forward in the line, but each person in front of you charges a specific amount to swap places. The cost to swap with person i is given by cost[i].
You are allowed to swap places with people as follows:
Return an array answer of size n, where answer[i] is the minimum total cost to reach each position i in the line.
Examples
Example 1
Input: cost = [5,3,4,1,3,2]
Output: [5,3,3,1,1,1]
Explanation:
We can get to each position in the following way:
Example 2
Input: cost = [1,2,4,6,7]
Output: [1,1,1,1,1]
Explanation:
We can swap with person 0 for a cost of 1, then we will be able to reach any position i for free.
Constraints
1 <= n == cost.length <= 100
1 <= cost[i] <= 100
Minimum Cost to Reach Every Position - Practice Coding | SlaveCode