3502. Minimum Cost to Reach Every Position
You are given an integer array
costof sizen. You are currently at positionn(at the end of the line) in a line ofn + 1people (numbered from 0 ton).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
iis given bycost[i].You are allowed to swap places with people as follows:
- If they are in front of you, you must pay them
cost[i]to swap with them.- If they are behind you, they can swap with you for free.
Return an array
answerof sizen, whereanswer[i]is the minimum total cost to reach each positioniin the line.
1 | class Solution { |