3502. Minimum Cost to Reach Every Position LeetCode Solution

In this guide, you will get 3502. Minimum Cost to Reach Every Position LeetCode Solution with the best time and space complexity. The solution to Minimum Cost to Reach Every Position problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Cost to Reach Every Position solution in C++
  4. Minimum Cost to Reach Every Position solution in Java
  5. Minimum Cost to Reach Every Position solution in Python
  6. Additional Resources
3502. Minimum Cost to Reach Every Position LeetCode Solution image

Problem Statement of Minimum Cost to Reach Every Position

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:

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 answer of size n, where answer[i] is the minimum total cost to reach each position i in the line.

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:

i = 0. We can swap with person 0 for a cost of 5.
i = 1. We can swap with person 1 for a cost of 3.
i = 2. We can swap with person 1 for a cost of 3, then swap with person 2 for free.
i = 3. We can swap with person 3 for a cost of 1.
i = 4. We can swap with person 3 for a cost of 1, then swap with person 4 for free.
i = 5. We can swap with person 3 for a cost of 1, then swap with person 5 for free.

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3502. Minimum Cost to Reach Every Position LeetCode Solution in C++

class Solution {
 public:
  vector<int> minCosts(vector<int>& cost) {
    vector<int> ans;
    int minCost = INT_MAX;
    for (const int c : cost) {
      minCost = min(minCost, c);
      ans.push_back(minCost);
    }
    return ans;
  }
};
/* code provided by PROGIEZ */

3502. Minimum Cost to Reach Every Position LeetCode Solution in Java

class Solution {
  public int[] minCosts(int[] cost) {
    int[] ans = new int[cost.length];
    int minCost = Integer.MAX_VALUE;
    for (int i = 0; i < cost.length; ++i) {
      minCost = Math.min(minCost, cost[i]);
      ans[i] = minCost;
    }
    return ans;
  }
}
// code provided by PROGIEZ

3502. Minimum Cost to Reach Every Position LeetCode Solution in Python

class Solution:
  def minCosts(self, cost: list[int]) -> list[int]:
    return list(itertools.accumulate(cost, min))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.