easyArray 0 views

Minimum Cost to Reach Every Position

You are given an integer array cost of size n.

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.

Minimum Cost to Reach Every Position diagram

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

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.