question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
minimum-cost-to-reach-every-position | One Linear - The Shortest Possible | one-linear-the-shortest-possible-by-char-w0i1 | null | charnavoki | NORMAL | 2025-03-31T07:08:55.233536+00:00 | 2025-03-31T07:08:55.233536+00:00 | 62 | false |
```javascript []
const minCosts = a =>
a.map((v, i) => a[i] = Math.min(v, a[i - 1] ?? a[0]));
``` | 3 | 0 | ['JavaScript'] | 0 |
minimum-cost-to-reach-every-position | 🌟 Simplest Solution For Beginners 💯🔥🗿 | simplest-solution-for-beginners-by-emman-4pci | Code | emmanuel011 | NORMAL | 2025-03-30T15:14:40.803411+00:00 | 2025-03-30T15:14:40.803411+00:00 | 94 | false | # Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
for i in range(1, len(cost)):
if cost[i] > cost[i - 1]:
cost[i] = cost[i - 1]
return cost
``` | 3 | 0 | ['Python3'] | 3 |
minimum-cost-to-reach-every-position | 🚀🚀Simple And Easy to Understand Code with || O(n)Time Complexity || 🔥🔥O(1)Space ||100%Faster | simple-and-easy-to-understand-code-with-jq2bj | IntuitionWe have a list of costs (numbers in an array).We want to adjust the costs based on some rule.
The code compares neighboring numbers and changes them if | rao_aditya | NORMAL | 2025-03-30T05:38:23.963473+00:00 | 2025-03-30T13:48:38.147624+00:00 | 481 | false | # Intuition
We have a list of costs (numbers in an array).We want to adjust the costs based on some rule.
The code compares neighboring numbers and changes them if needed.
It looks like it’s trying to keep costs as low as possible while moving from left to right.
# Approach
Start with a list of costs (e.g., [3, 5, 2, 4]).
Look at each pair of numbers next to each other:
Check the current number (cost[i]) and the next number (cost[i+1]).
If the current number is smaller or equal (e.g., 3 <= 5), make the next number the same as the current number (e.g., change 5 to 3).
Move to the next pair and repeat until the end of the list.
Return the updated list (e.g., [3, 3, 2, 2]).
# Complexity
- Time complexity:
$$O(n)$$
"n" is the number of costs in the list.
We only go through the list once, checking each number.
It’s fast because we don’t repeat steps—it’s just one straight pass.
- Space complexity:
$$O(1)$$
We don’t use extra space (like new lists).
We change the original list directly and only use a couple of small variables (like i).
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n=cost.size();
int i=0;
while(i<n-1){
if(cost[i]<=cost[i+1]){
cost[i+1]=cost[i];
}
i++;
}
return cost;
}
};
``` | 3 | 0 | ['C++'] | 3 |
minimum-cost-to-reach-every-position | Simple explanation and beginner friendly solution✅✅✅ | simple-explanation-and-beginner-friendly-t2hy | Intuition:We need to create a new array where each element is the minimum cost up to that position.Approach:
Start by setting the first element as the first cos | nilestiwari_7 | NORMAL | 2025-03-30T04:14:18.310994+00:00 | 2025-03-30T07:27:32.534424+00:00 | 251 | false | ### Intuition:
We need to create a new array where each element is the minimum cost up to that position.
### Approach:
- Start by setting the first element as the first cost.
- For each subsequent element, keep track of the smallest cost found so far and store it.
### Complexity:
- **Time complexity:** \(O(n)\)
- **Space complexity:** \(O(n)\)
### Code:
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n = cost.size();
vector<int> ans(n, 0);
ans[0] = cost[0];
int minCost = cost[0];
for (int i = 1; i < n; i++) {
minCost = min(minCost, cost[i]);
ans[i] = minCost;
}
return ans;
}
};
```
``` python []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
n = len(cost)
ans = [0] * n
ans[0] = cost[0]
min_cost = cost[0]
for i in range(1, n):
min_cost = min(min_cost, cost[i])
ans[i] = min_cost
return ans
```
``` javascript []
/**
* @param {number[]} cost
* @return {number[]}
*/
var minCosts = function(cost) {
let n = cost.length;
let ans = new Array(n).fill(0);
ans[0] = cost[0];
let minCost = cost[0];
for (let i = 1; i < n; i++) {
minCost = Math.min(minCost, cost[i]);
ans[i] = minCost;
}
return ans;
};
``` | 3 | 0 | ['Greedy', 'Simulation', 'C++', 'Python3', 'JavaScript'] | 0 |
minimum-cost-to-reach-every-position | EASY SOLUTION IN CPP | easy-solution-in-cpp-by-harrshinisg-js2f | IntuitionThe problem requires computing the minimum value encountered so far in the cost array at each index. This can be efficiently solved using a single pass | harrshinisg | NORMAL | 2025-04-01T14:42:41.397782+00:00 | 2025-04-01T14:42:41.397782+00:00 | 97 | false | # Intuition
The problem requires computing the minimum value encountered so far in the cost array at each index. This can be efficiently solved using a single pass through the array while keeping track of the minimum value seen so far.
# Approach
1.)Initialize min_so_far with the first element of cost, as it represents the starting minimum.
2.)Create an answer array of the same size as cost, initialized with zeros.
3.)Iterate through the cost array:
4.)Update min_so_far to maintain the smallest value encountered up to that index.
5.)Store min_so_far in the corresponding index of the answer array.
6.)Return the answer array after processing all elements.
# Complexity
-** Time complexity:** :-O(n)
-** Space complexity:** :-O(n)(can be O(1) if modified in place)
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n=cost.size();
vector<int>answer(n,0);
int min_so_far=cost[0];
for(int i=0;i<n;i++){
min_so_far=min(min_so_far,cost[i]);
answer[i]=min_so_far;
}
return answer;
}
};
``` | 2 | 0 | ['C++'] | 2 |
minimum-cost-to-reach-every-position | Java Running Minimum Solution | java-running-minimum-solution-by-tbekpro-y1nf | Complexity
Time complexity: O(N)
Space complexity: O(1)
Code | tbekpro | NORMAL | 2025-03-30T16:23:45.766053+00:00 | 2025-03-30T16:23:45.766053+00:00 | 47 | false | # Complexity
- Time complexity: O(N)
- Space complexity: O(1)
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int min = cost[0];
for (int i = 1; i < cost.length; i++) {
min = Math.min(min, cost[i]);
cost[i] = min;
}
return cost;
}
}
``` | 2 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | 🔥 Very Easy Detailed Solution || 🎯 JAVA || 🎯 C++ | very-easy-detailed-solution-java-c-by-ch-8d70 | Approach
The function minCosts takes an array cost and returns an array answer, where answer[i] stores the minimum cost encountered from the start up to index | chaturvedialok44 | NORMAL | 2025-03-30T15:55:24.146141+00:00 | 2025-03-30T15:55:24.146141+00:00 | 176 | false | # Approach
<!-- Describe your approach to solving the problem. -->
1. The function minCosts takes an array cost and returns an array answer, where answer[i] stores the minimum cost encountered from the start up to index i.
2. **Initialization:**
- The answer array is initialized with Integer.MAX_VALUE.
3. **Traversal:**
- For each index i, set answer[i] = cost[i] (assign current cost value).
- If i > 0, update answer[i] as the minimum of answer[i] and answer[i - 1]. This ensures answer[i] holds the minimum cost from index 0 to i.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$O(n)$
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$O(n)$
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int n = cost.length;
int[] answer = new int[n];
Arrays.fill(answer, Integer.MAX_VALUE);
for (int i = 0; i < n; i++) {
answer[i] = cost[i];
if (i > 0) answer[i] = Math.min(answer[i], answer[i - 1]);
}
return answer;
}
}
```
```C++ []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n = cost.size();
std::vector<int> answer(n, INT_MAX);
for (int i = 0; i < n; i++) {
answer[i] = cost[i];
if (i > 0) answer[i] = std::min(answer[i], answer[i - 1]);
}
return answer;
}
};
``` | 2 | 0 | ['Array', 'Math', 'Simulation', 'C++', 'Java'] | 2 |
minimum-cost-to-reach-every-position | Newbie Solution. | newbie-solution-by-szzznotpro-gnct | IntuitionApproachWe know that exchanging places with the first person has no other possible outcomes other than paying him.Hence, final.append(cost[0])For the o | szzznotpro | NORMAL | 2025-03-30T07:51:43.460914+00:00 | 2025-03-30T07:51:43.460914+00:00 | 66 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
We know that exchanging places with the first person has no other possible outcomes other than paying him.
Hence, final.append(cost[0])
For the other people however, compare the lowest cost taken to exchange with the people in front, and the cost to exchange with them. The minimal is the cost needed.
Newbie solution.
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
final=[]
final.append(cost[0])
for i in range(1,len(cost)):
if cost[i] <= min(final):
final.append(cost[i])
else:
final.append(min(final))
return final
``` | 2 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | Beats 100% | Easy C++ solution | beats-100-easy-c-solution-by-anuragpal01-p3yv | Intuition
The problem requires us to determine the minimum total cost to reach each position in the line. The key observation is:
Moving forward (towards index | Anuragpal010104 | NORMAL | 2025-03-30T07:21:34.069809+00:00 | 2025-03-30T07:21:34.069809+00:00 | 101 | false | # Intuition
- The problem requires us to determine the minimum total cost to reach each position in the line. The key observation is:
- Moving forward (towards index 0) requires paying cost[i].
- Moving backward is free.
- The goal is to minimize the total cost required to reach each position i.
- By analyzing the movement rules:
- The cost of reaching position i is dictated by the minimum cost seen so far from i to n-1 (since moving back is free, we only care about the lowest cost seen).
- For each position i, we just need to know the minimum swap cost of all the positions ahead of it.
Example Dry Run
Let's take the given example:
Input:
cost=[5,3,4,1,3,2]
Step-by-Step Dry Run
We initialize minsfr = ∞ and iterate through the array while keeping track of the minimum cost seen so far.
Index i cost[i] Minimum cost seen so far (minsfr) Updated cost[i]
0 5 min(∞, 5) = 5 5
1 3 min(5, 3) = 3 3
2 4 min(3, 4) = 3 3
3 1 min(3, 1) = 1 1
4 3 min(1, 3) = 1 1
5 2 min(1, 2) = 1 1
Final Output:
[5,3,3,1,1,1]
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
- We traverse the given cost array from left to right, maintaining a running minimum swap cost (minsfr).
- At each index:
- Update minsfr: Keep track of the minimum swap cost encountered so far.
- Update cost[i]: Replace cost[i] with minsfr, as this represents the minimum cost required to reach position i.
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(n), since we iterate through the cost array exactly once.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(1), as we modify the input array in place without using extra memory.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int minsfr = INT_MAX;
int n=cost.size();
for (int i = 0; i < n; i++) {
minsfr = min(minsfr, cost[i]);
cost[i] = minsfr;
}
return cost;
}
};
``` | 2 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | easy cpp solution | easy-cpp-solution-by-akshkhurana-0adx | IntuitionWe need to determine the minimum cost to reach each position in the line, starting from the last position (n). Moving forward is free, but swapping wit | akshkhurana | NORMAL | 2025-03-30T04:07:17.205319+00:00 | 2025-03-30T04:16:17.960512+00:00 | 58 | false | # Intuition
We need to determine the minimum cost to reach each position in the line, starting from the last position (n). Moving forward is free, but swapping with someone ahead costs cost[i]. Our goal is to compute the optimal cost at each position while considering these rules.
# Approach:
Initialize a dp array where dp[n] = 0 (since no cost is needed at the last position).
Iterate backwards from n-1 to 0, updating dp[i] by taking the minimum of:
Staying in place (dp[i+1]), which is free.
Swapping with the person ahead (dp[i+1] + cost[i]).
Return the computed dp array excluding dp[n].
# Complexity
- Time complexity: O(n)
- Space complexity: Since dp is the only additional storage, the space complexity is: O(n)
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n = cost.size();
vector<int> dp(n, 0);
dp[0] = cost[0];
for (int i = 1; i < n; ++i) {
dp[i] = min(dp[i - 1], cost[i]);
}
return dp;
}
};
``` | 2 | 0 | ['C++'] | 1 |
minimum-cost-to-reach-every-position | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-okxfy | Code | DanisDeveloper | NORMAL | 2025-04-06T08:44:14.418331+00:00 | 2025-04-06T08:44:14.418331+00:00 | 11 | false |
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
m = cost[0]
res = []
for i in range(len(cost)):
m = min(cost[i], m)
res.append(m)
return res
``` | 1 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | 🚀 Best Python Solution | Beats 99% Solutions | Beginner Friendly | best-python-solution-beats-99-solutions-zoqp3 | IntuitionThe problem requires us to return an array where each index i holds the minimum value from the start up to index i in the given cost array.To solve thi | PPanwar29 | NORMAL | 2025-04-06T04:09:31.022206+00:00 | 2025-04-06T04:09:31.022206+00:00 | 13 | false | # Intuition
The problem requires us to return an array where each index i holds the minimum value from the start up to index i in the given cost array.
To solve this, we can keep track of the running minimum as we iterate through the list and append it to the result array
# Approach
Initialize an empty list ans to store the results.
Use a variable min_val to track the minimum value seen so far, initialized to infinity.
Iterate over each element in the input list cost.
At each step, update min_val with the smaller of the current value and the previous min_val.
Append min_val to the result list.
Return the result list.
# Complexity
Time complexity: O(n)
We iterate through the list once, where n is the length of cost.
Space complexity: O(n)
We store the result in a new list of size n.
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
n = len(cost)
ans=[]
min_val= float('inf')
for i in range(len(cost)):
min_val= min(min_val,cost[i])
ans.append(min_val)
return ans
``` | 1 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | 🚀 Tracking the Cheapest Path: A Rolling Minimum Approach! 🔥 | tracking-the-cheapest-path-a-rolling-min-j011 | IntuitionThe problem requires us to compute the minimum cost up to each index in the given list. This means that for every position i, we want to know the small | Trippy_py | NORMAL | 2025-04-04T15:32:06.694124+00:00 | 2025-04-04T15:32:06.694124+00:00 | 16 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to compute the minimum cost up to each index in the given list. This means that for every position i, we want to know the smallest value encountered from the beginning (index 0) to i.
# Approach
<!-- Describe your approach to solving the problem. -->
1) Initialize the minimum cost variable (minCost) with the first element of the array.
2) Iterate through the array from index 1 to the end:
Update minCost if the current element is smaller.
Update cost[i] to store the minimum cost encountered so far.
3) Return the modified cost array, which now contains the minimum values seen up to each index.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
The algorithm runs in O(n) since it traverses the array once.
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1) as the solution modifies the input array in place without extra space.
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
minCost = cost[0]
for i in range(1,len(cost)):
if cost[i]<minCost:
minCost = cost[i]
cost[i] = minCost
return cost
``` | 1 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | 0ms 100% O(N) std::min | 0ms-100-on-stdmin-by-michelusa-mb9c | We can use std::min to keep track of the least expensive swap.Space + Time complexity: O(N)Code | michelusa | NORMAL | 2025-04-02T22:04:38.657932+00:00 | 2025-04-02T22:04:38.657932+00:00 | 17 | false | We can use std::min to keep track of the least expensive swap.
Space + Time complexity: O(N)
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(const std::vector<int>& cost) {
std::vector<int> result;
result.reserve(cost.size());
int min_swap_cost = cost.front();
for (int value : cost) {
min_swap_cost = std::min(min_swap_cost, value);
result.push_back(min_swap_cost);
}
return result;
}
};
``` | 1 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | ✅ 2-Liner 🚀💻BEATS 100%⌛ | 2-liner-beats-100-by-sohamkhanna-uiha | IntuitionThe problem requires us to modify the given array such that each element is the minimum of itself and the previous element. This ensures that every ele | sohamkhanna | NORMAL | 2025-04-02T15:33:30.216863+00:00 | 2025-04-02T15:33:30.216863+00:00 | 9 | false | # Intuition
The problem requires us to modify the given array such that each element is the minimum of itself and the previous element. This ensures that every element in the array is non-increasing as we traverse from left to right.
# Approach
- We iterate through the `cost` array starting from index `1` to `n-1`.
- At each index `i`, we update `cost[i]` to be the minimum of `cost[i]` and `cost[i-1]`.
- This guarantees that each element is at most the previous element.
- Finally, we return the modified `cost` array.
# Complexity
- **Time complexity:** $$O(n)$$ since we traverse the array once, where `n` is the size of the `cost` array.
- **Space complexity:** $$O(1)$$ as the algorithm modifies the input array in place and does not use any extra space.
# Code
```cpp
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
for(int i=1; i<cost.size(); i++) {
cost[i] = min(cost[i], cost[i-1]);
}
return cost;
}
};
| 1 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | Minimum Cost to Reach Any Position, the Efficient Way | minimum-cost-to-reach-any-position-the-e-d3ba | IntuitionHmm... Find minimum costs to reach each position we must. Like choosing the cheapest path through a market, we'll track the lowest cost encountered so | x7Fg9_K2pLm4nQwR8sT3vYz5bDcE6h | NORMAL | 2025-04-02T14:48:20.179915+00:00 | 2025-04-02T14:48:20.179915+00:00 | 18 | false | # Intuition
Hmm... Find minimum costs to reach each position we must. Like choosing the cheapest path through a market, we'll track the lowest cost encountered so far.
# Approach
1. Initialize result array: First position's cost is simply cost[0]
2. Iterate through costs: For each position, the minimum cost is either:
- The cost to swap directly with that person (cost[i])
- The minimum cost from the previous position (ans[i-1])
3. Build answer array: Store the minimum of these two values at each position
# Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(n)$$
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
vector<int>ans(cost.size(),cost[0]);
for (int i=1;i<cost.size();i++){ans[i]=min(ans[i-1],cost[i]);}
return ans;
}
};
``` | 1 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | EASY APPROACH WITH BASIC IDEA O(N) beats 100% | easy-approach-with-basic-idea-on-beats-1-vnaq | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Yathish_2006 | NORMAL | 2025-03-31T16:49:13.532622+00:00 | 2025-03-31T16:49:13.532622+00:00 | 14 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n=cost.size();
vector<int> res(n,0);
res[0]=cost[0];
for(int i=1;i<cost.size();i++){
if(cost[i]<res[i-1]){
res[i]=cost[i];
}
else{
res[i]=res[i-1];
}
}
return res;
}
};
``` | 1 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | C++ simple solution | c-simple-solution-by-ralf_phi-6118 | ApproachMove forward to i requires cost[i], while move back requires nothing, in order to find the minimal cost of each position, we just need to find the minim | ralf_phi | NORMAL | 2025-03-31T11:53:15.727785+00:00 | 2025-03-31T11:53:15.727785+00:00 | 11 | false | # Approach
Move forward to i requires cost[i], while move back requires nothing, in order to find the minimal cost of each position, we just need to find the minimal cost in front of it (inclusive), then move back for free.
We only need one traversal, and update the smallest cost along with the traversal duriing each position, then fill it into the cost of ith position.
Note that as each position will be traversed only once, it is un-necessary to create another array for it, just in-place updates, besides in case of (cost[i] == curr) there is no need to update cost[i] with curr, while it is still correct by doing do.
# Complexity
- Time complexity:
O(N)
- Space complexity:
O(1)
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int curr = INT_MAX;
for (int i = 0; i < cost.size(); i++) {
if (cost[i] < curr) curr = cost[i];
else if (cost[i] > curr) cost[i] = curr;
}
return cost;
}
};
``` | 1 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | Brute Force | Better | Optimal | Beginner Friendly Solution ✅✅| C++ | Java | Python | JavaScript | brute-force-better-optimal-beginner-frie-x0xf | IntuitionIn this problem, it basically compares neighboring numbers and changes them if needed.1st Approach(Brute Force Solution)CodeComplexity
Time complexity: | souvikpramanik874 | NORMAL | 2025-03-30T22:01:30.184915+00:00 | 2025-03-30T22:01:30.184915+00:00 | 46 | false | # Intuition
In this problem, it basically compares neighboring numbers and changes them if needed.
# 1st Approach(Brute Force Solution)
<!-- Describe your approach to solving the problem. -->
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
for(int i=1;i<cost.size();i++){
int mini=INT_MAX;
for(int j=0;j<=i;j++){
mini=min(mini,cost[j]);
}
cost[i]=mini;
}
return cost;
}
};
```
```java []
class Solution {
public int[] minCosts(int[] cost) {
for(int i=1;i<cost.length;i++){
int mini=Integer.MAX_VALUE;
for(int j=0;j<=i;j++){
mini=Math.min(mini,cost[j]);
}
cost[i]=mini;
}
return cost;
}
}
```
```python []
class Solution(object):
def minCosts(self, cost):
for i in range(1, len(cost)):
cost[i] = min(cost[:i+1])
return cost
```
```javascript []
/**
* @param {number[]} cost
* @return {number[]}
*/
var minCosts = function(cost) {
for (let i = 1; i < cost.length; i++) {
cost[i] = Math.min(...cost.slice(0, i + 1));
}
return cost;
};
```

# Complexity
- Time complexity: 0(n^2)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: 0(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# 2nd Approach(Better Solution)
<!-- Describe your approach to solving the problem. -->
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
stack<int> st;
st.push(cost[0]);
for(int i=1;i<cost.size();i++){
if(st.top()>cost[i]){
st.pop();
st.push(cost[i]);
}
else{
cost[i]=st.top();
}
}
return cost;
}
};
```
```java []
class Solution {
public int[] minCosts(int[] cost) {
Stack<Integer> st = new Stack<>();
st.push(cost[0]);
for (int i = 1; i < cost.length; i++) {
if (st.peek() > cost[i]) {
st.pop();
st.push(cost[i]);
} else {
cost[i] = st.peek();
}
}
return cost;
}
}
```
```python []
class Solution(object):
def minCosts(self, cost):
stack = [cost[0]]
for i in range(1, len(cost)):
if stack[-1] > cost[i]:
stack.pop()
stack.append(cost[i])
else:
cost[i] = stack[-1]
return cost
```
```javascript []
/**
* @param {number[]} cost
* @return {number[]}
*/
var minCosts = function(cost) {
let stack = [cost[0]];
for (let i = 1; i < cost.length; i++) {
if (stack[stack.length - 1] > cost[i]) {
stack.pop();
stack.push(cost[i]);
} else {
cost[i] = stack[stack.length - 1];
}
}
return cost;
};
```

# Complexity
- Time complexity:0(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:0(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# 3rd Approach(Optimal Solution)
<!-- Describe your approach to solving the problem. -->
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int mini=cost[0];
for(int i=1;i<cost.size();i++){
if(mini>=cost[i]){
mini=cost[i];
}
else{
cost[i]=mini;
}
}
return cost;
}
};
```
```java []
class Solution {
public int[] minCosts(int[] cost) {
int minVal = cost[0];
for (int i = 1; i < cost.length; i++) {
if (minVal >= cost[i]) {
minVal = cost[i];
} else {
cost[i] = minVal;
}
}
return cost;
}
}
```
```python []
class Solution(object):
def minCosts(self, cost):
min_val = cost[0]
for i in range(1, len(cost)):
if min_val >= cost[i]:
min_val = cost[i]
else:
cost[i] = min_val
return cost
```
```javascript []
/**
* @param {number[]} cost
* @return {number[]}
*/
var minCosts = function(cost) {
let minVal = cost[0];
for (let i = 1; i < cost.length; i++) {
if (minVal >= cost[i]) {
minVal = cost[i];
} else {
cost[i] = minVal;
}
}
return cost;
};
```

# Complexity
- Time complexity:0(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:0(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ --> | 1 | 0 | ['Stack', 'Simulation', 'Python', 'C++', 'Java', 'JavaScript'] | 0 |
minimum-cost-to-reach-every-position | EASY C++ 100% O(N) | easy-c-100-on-by-hnmali-jhzd | Code | hnmali | NORMAL | 2025-03-30T17:56:02.845711+00:00 | 2025-03-30T17:56:02.845711+00:00 | 8 | false |
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
vector<int> ans(cost.size());
ans[0] = cost[0];
int minm = cost[0];
for(int i = 1; i < cost.size(); i++) {
if(cost[i] < minm)
minm = cost[i];
ans[i] = minm;
}
return ans;
}
};
``` | 1 | 0 | ['Array', 'Simulation', 'C++'] | 0 |
minimum-cost-to-reach-every-position | Simple C++ Solution | Weekly Contest 443 | simple-c-solution-weekly-contest-443-by-nudxm | IntuitionComplexity
Time complexity:
O(n)
Space complexity:
O(1)
Code | ipriyanshi | NORMAL | 2025-03-30T09:47:43.134152+00:00 | 2025-03-30T09:47:43.134152+00:00 | 11 | false | # Intuition
https://youtu.be/_qqFep8xPcE
# Complexity
- Time complexity:
$$O(n)$$
- Space complexity:
$$O(1)$$
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n = cost.size();
vector<int> result(n);
result[0] = cost[0];
for(int i = 1; i < n; i++){
result[i] = min(result[i-1], cost[i]);
}
return result;
}
};
``` | 1 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | beginner friendly using a reference and checking by if | beginner-friendly-using-a-reference-and-pdoqn | IntuitionApproachComplexity
Time complexity:
0 ms O[N]
Space complexity:
Code | vijeyabinessh | NORMAL | 2025-03-30T09:02:11.512557+00:00 | 2025-03-30T09:02:11.512557+00:00 | 10 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
0 ms O[N]
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int cur=cost[0];
vector<int>ans;
for(int i =0;i<cost.size();i++){
if(cost[i]>=cur){
ans.push_back(cur);
}
else{
cur=cost[i];
ans.push_back(cost[i]);
}
}
return ans;
}
};
``` | 1 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | Optimal Solution || beats 100% | optimal-solution-by-sumitksr-0mhh | Code | sumitksr | NORMAL | 2025-03-30T07:51:53.123840+00:00 | 2025-03-30T07:52:13.600565+00:00 | 15 | false |
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n = cost.size();
vector<int> ans(n);
int mini = cost[0];
ans[0] = mini;
for (int i = 1; i < n; ++i) {
mini = min(mini, cost[i]);
ans[i] = mini;
}
return ans;
}
};
``` | 1 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | Weekly contest 443 solution | weekly-contest-443-solution-by-mukundan_-qtoi | IntuitionApproachComplexity
Time complexity:O(N)
Space complexity:O(N)
Code | Mukundan_13 | NORMAL | 2025-03-30T05:46:51.914836+00:00 | 2025-03-30T05:46:51.914836+00:00 | 31 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int n=cost.length;
int[] res=new int[n];
int mini=Integer.MAX_VALUE;
for(int i=0;i<n;i++)
{
mini=Math.min(mini,cost[i]);
res[i]=mini;
}
return res;
}
}
``` | 1 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Simple In-Place Solution. | simple-in-place-solution-by-varun_cns-saaa | IntuitionApproachComplexity
Time complexity: 0(n)
Space complexity: 0(1)
Code | varun_cns | NORMAL | 2025-03-30T05:41:52.929624+00:00 | 2025-03-30T05:41:52.929624+00:00 | 17 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: 0(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: 0(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int min= Integer.MAX_VALUE;
for(int i=0;i<cost.length;i++){
if(cost[i]<min){
min=cost[i];
}
else{
cost[i]=min;
}
}
return cost;
}
}
``` | 1 | 0 | ['Greedy', 'Java'] | 0 |
minimum-cost-to-reach-every-position | Efficient Array Processing: Tracking the Minimum at Each Step|| Easy Approach || Beginer Friendly | efficient-array-processing-tracking-the-osrer | IntuitionThe goal is to determine the minimum cost encountered so far for each index in the array. This means that for every position i, we need to find the min | franesh | NORMAL | 2025-03-30T05:20:50.982024+00:00 | 2025-03-30T05:20:50.982024+00:00 | 17 | false | ## **Intuition**
The goal is to determine the minimum cost encountered so far for each index in the array. This means that for every position `i`, we need to find the minimum value from index `0` to `i`.
A simple way to achieve this is by maintaining a running minimum while iterating through the array. This ensures that at every step, we already have the minimum cost up to that point.
---
## **Approach**
1. **Initialize** an empty answer array `answer` of the same size as `cost` to store the results.
2. **Maintain a variable** `min_cost_so_far`, initialized to a very large value (`INT_MAX`), which keeps track of the minimum value encountered while iterating.
3. **Iterate through the array** from left to right:
- Update `min_cost_so_far` by taking the minimum of the current element `cost[i]` and `min_cost_so_far`.
- Store this minimum value in `answer[i]`.
4. **Return the `answer` array**, which contains the minimum costs encountered at each index.
---
## **Complexity Analysis**
- **Time Complexity:**
- The algorithm iterates through the array once, updating `min_cost_so_far` in each step.
- **O(n)** where `n` is the size of the input array `cost`.
- **Space Complexity:**
- The solution only uses an additional array `answer` of size `n`, leading to **O(n)** extra space.
- If we modify the input array instead of using a new array, space complexity can be reduced to **O(1)**.
---
## **Code**
```cpp
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n = cost.size();
vector<int> answer(n);
int min_cost_so_far = INT_MAX;
for (int i = 0; i < n; i++) {
min_cost_so_far = min(min_cost_so_far, cost[i]);
answer[i] = min_cost_so_far;
}
return answer;
}
};
```
---
## **Key Takeaways**
✅ Uses a single pass (`O(n)`) for efficiency.
✅ Maintains a running minimum to avoid unnecessary nested loops.
✅ Simple and readable solution with minimal extra space usage. | 1 | 0 | ['Array', 'Greedy', 'Simulation', 'C++'] | 0 |
minimum-cost-to-reach-every-position | Return min of all costs till index i | return-min-of-all-costs-till-index-i-by-zwcxq | IntuitionThe optimal cost to reach place i is the minimum of all costs till index i. Why? Because we can swap with the minimum cost index that comes on or befor | akshar_ | NORMAL | 2025-03-30T04:48:38.437739+00:00 | 2025-03-30T04:48:38.437739+00:00 | 29 | false | # Intuition
The optimal cost to reach place $i$ is the minimum of all costs till index $i$. Why? Because we can swap with the minimum cost index that comes on or before index $i$ and then swap with 0 cost to reach $i$ (if needed)
# Complexity
- Time complexity:
$$O(n)$$
- Space complexity:
$$O(n)$$ to store answer
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n = cost.size();
vector<int> mins(n);
mins[0] = cost[0];
for (int i = 1; i < n; i++) {
mins[i] = min(mins[i - 1], cost[i]);
}
return mins;
}
};
``` | 1 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | Beginner Friendly | Easy to understand | beginner-friendly-easy-to-understand-by-2dtrp | IntuitionThis solution is similar to finding prefix minimum.Code | alishershaesta | NORMAL | 2025-03-30T04:15:11.927878+00:00 | 2025-03-30T04:15:11.927878+00:00 | 12 | false | # Intuition
This solution is similar to finding prefix minimum.
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
ln =len(cost)
for c in range(1, ln):
cost[c] = min(cost[c],cost[c-1])
return cost
``` | 1 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | Beats 100% | Prefix minimum | beats-100-prefix-minimum-by-parikshit-aryn | Code | parikshit_ | NORMAL | 2025-03-30T04:08:30.712250+00:00 | 2025-03-30T04:08:30.712250+00:00 | 18 | false |
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
mn = float("inf")
result = [0] * len(cost)
for i in range(len(cost)):
if cost[i] < mn:
mn = cost[i]
result[i] = mn
return result
``` | 1 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | ✅ 🌟 JAVA SOLUTION ||🔥 BEATS 100% PROOF🔥|| 💡 CONCISE CODE ✅ || 🧑💻 BEGINNER FRIENDLY | java-solution-beats-100-proof-concise-co-kas7 | Complexity
Time complexity:O(n)
Space complexity:O(1)
Code | Shyam_jee_ | NORMAL | 2025-03-30T04:07:32.587369+00:00 | 2025-03-30T04:07:32.587369+00:00 | 40 | false | # Complexity
- Time complexity:$$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:$$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int minCost=cost[0];
int[] ans=new int[cost.length];
ans[0]=minCost;
for(int i=1;i<cost.length;i++)
{
minCost=Math.min(cost[i], minCost);
ans[i]=minCost;
}
return ans;
}
}
``` | 1 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | 100% Affective Java Code | 100-affective-java-code-by-anuragk2-nfk1 | IntuitionApproachComplexity
Time complexity:
O(b)
Space complexity:
O(b)
Code | anuragk2 | NORMAL | 2025-03-30T04:05:06.188743+00:00 | 2025-03-30T04:05:06.188743+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
O(b)
- Space complexity:
O(b)
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int b = cost.length;
int[] a = new int[b];
int m = Integer.MAX_VALUE;
for (int i = 0; i < b; i++) {
m = Math.min(m, cost[i]);
a[i] = m;
}
return a;
}
}
``` | 1 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Simple Solution : O(N) | simple-solution-on-by-ankith_kumar_singh-7sv7 | Code | Ankith_Kumar_Singh | NORMAL | 2025-03-30T04:03:31.912981+00:00 | 2025-03-30T04:03:31.912981+00:00 | 7 | false |
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int min = cost[0];
int[] ans = new int[cost.length];
for(int i = 0;i<cost.length;i++){
min = Math.min(min,cost[i]);
ans[i] = min;
}
return ans;
}
}
``` | 1 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Easy Solution || Java || Python || C++ || Beats 100.00% 👋 || 🚀🚀 || 🔥🔥 | easy-solution-java-python-c-beats-10000-thxpx | IntuitionApproachComplexity
Time complexity: O(N)
Space complexity: O(1)
Code | vermaanshul975 | NORMAL | 2025-03-30T04:02:32.710970+00:00 | 2025-03-30T04:02:32.710970+00:00 | 25 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int n = cost.length;
int[] res = new int[n];
int minCost = Integer.MAX_VALUE;
for(int i = 0;i<n;i++){
minCost = Math.min(minCost,cost[i]);
res[i] = minCost;
}
return res;
}
}
```
```python []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
n = len(cost)
res = [0] * n
min_cost = float('inf')
for i in range(n):
min_cost = min(min_cost, cost[i])
res[i] = min_cost
return res
```
```C++ []
class Solution {
public:
std::vector<int> minCosts(std::vector<int>& cost) {
int n = cost.size();
std::vector<int> res(n);
int minCost = INT_MAX;
for (int i = 0; i < n; i++) {
minCost = std::min(minCost, cost[i]);
res[i] = minCost;
}
return res;
}
};
```
| 1 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Simple and elegant python solution | simple-and-elegant-python-solution-by-vi-0x4x | Intuition behind this solution is to find previous small elementCode | vigneshkanna108 | NORMAL | 2025-03-30T04:02:26.446892+00:00 | 2025-03-30T04:02:26.446892+00:00 | 45 | false |
Intuition behind this solution is to find previous small element
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
for i in range(1, len(cost)):
cost[i] = min(cost[i], cost[i-1])
return cost
``` | 1 | 0 | ['Java', 'Python3', 'JavaScript'] | 0 |
minimum-cost-to-reach-every-position | || ✅#DAY_71th_of_Daily_Coding && Optimal Approach✅ || | day_71th_of_daily_coding-optimal-approac-w1ja | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Coding_With_Star | NORMAL | 2025-03-30T04:01:39.147009+00:00 | 2025-03-30T04:01:39.147009+00:00 | 30 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n = cost.size();
vector<int> answer(n);
int minCost = cost[0];
for (int i = 0; i < n; i++) {
minCost = min(minCost, cost[i]);
answer[i] = minCost;
}
return answer;
}
};
``` | 1 | 0 | ['C++', 'Java', 'Python3'] | 0 |
minimum-cost-to-reach-every-position | Simple and Easy Beginner Friendly Code | simple-and-easy-beginner-friendly-code-b-lu3r | IntuitionThe problem modifies the input list cost in-place. For each element starting from the second element, it compares the element with the minimum value of | Balarakesh | NORMAL | 2025-04-11T17:51:32.869227+00:00 | 2025-04-11T17:51:32.869227+00:00 | 1 | false | # Intuition
The problem modifies the input list `cost` in-place. For each element starting from the second element, it compares the element with the minimum value of all the elements up to that point. If the current element is greater than the minimum, it replaces the current element with the minimum.
# Approach
1. Iterate through the `cost` list starting from the second element (index 1) to the end.
2. For each element at index `i`, find the minimum value among the elements from index 0 to `i` (inclusive) using `min(cost[0:i+1])`.
3. Compare the current element `cost[i]` with the `minimum`.
4. If `cost[i]` is greater than `minimum`, replace `cost[i]` with `minimum`.
5. After the loop finishes, return the modified `cost` list.
# Complexity
- Time complexity:
$$O(n^2)$$ where n is the length of the input list `cost`. The outer loop iterates `n-1` times, and the `min(cost[0:i+1])` operation takes `O(i)` time in each iteration, which is `O(n)` in the worst case.
- Space complexity:
$$O(1)$$ because the modification is done in-place, and we are not using any extra space that scales with the input size.
# Code
```python3
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
for i in range(1, len(cost)):
minimum = min(cost[0:i+1])
if cost[i] > minimum:
cost[i] = minimum
return cost | 0 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | rust scan | rust-scan-by-mindulay-nvdn | Code | mindulay | NORMAL | 2025-04-11T14:57:12.706400+00:00 | 2025-04-11T14:57:12.706400+00:00 | 1 | false | # Code
```rust []
impl Solution {
pub fn min_costs(cost: Vec<i32>) -> Vec<i32> {
cost.into_iter().scan(i32::MAX, |s, x| {
*s = (*s).min(x);
Some(*s)
}).collect::<Vec<_>>()
}
}
``` | 0 | 0 | ['Rust'] | 0 |
minimum-cost-to-reach-every-position | 3502. Minimum Cost to Reach Every Position | 3502-minimum-cost-to-reach-every-positio-28zu | IntuitionApproachBasic ek pattern dikha tha waise hi implement kiyaaaComplexity
Time complexity: O(n)
Space complexity: O(n)
Code | tanmayyguptaa | NORMAL | 2025-04-10T16:33:36.290868+00:00 | 2025-04-10T16:33:36.290868+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
Basic ek pattern dikha tha waise hi implement kiyaaa
# Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int[] arr = new int[cost.length];
int min = cost[0] ;
for(int i = 0 ; i < cost.length ;i++){
if(cost[i] < min){
min = cost[i];
}
arr[i] = min ;
}
return arr ;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | ✅🔥Difficult wordplay Easy problem | Beats 100.00%🔥✅ | difficult-wordplay-easy-problem-beats-10-32cb | ApproachProblem is to find the minimum so far starting from the leftmost index :)Code | Qatyayani | NORMAL | 2025-04-09T17:48:33.933865+00:00 | 2025-04-09T17:48:33.933865+00:00 | 1 | false |
# Approach
Problem is to find the minimum so far starting from the leftmost index :)
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
ans=[]
s=1e9
for c in cost:
s=min(c,s)
ans.append(s)
return ans
``` | 0 | 0 | ['Array', 'Python3'] | 0 |
minimum-cost-to-reach-every-position | Simple solution -> Beats 96.94% | simple-solution-beats-9694-by-developers-8mcn | IntuitionApproachComplexity
Time complexity:
O(n)
Space complexity:
O(1)
Code | DevelopersUsername | NORMAL | 2025-04-09T16:42:02.774233+00:00 | 2025-04-09T16:42:02.774233+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
O(n)
- Space complexity:
O(1)
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
for (int i = 1; i < cost.length; i++)
cost[i] = Math.min(cost[i-1], cost[i]);
return cost;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | optimum solution for java | optimum-solution-for-java-by-rambabusai-los4 | Intuitionno intilizationApproachcost of 0th position is cost[0] forever.
for other positions we can check
if the current position(1) cost is greater than previo | rambabusai | NORMAL | 2025-04-09T15:33:20.079683+00:00 | 2025-04-09T15:33:20.079683+00:00 | 2 | false | # Intuition
no intilization
# Approach
cost of 0th position is cost[0] forever.
for other positions we can check
if the current position(1) cost is greater than previous position(0) cost
then
replace the cost of current position with cost of previous position in the same array
else do nothing
# Complexity
- Time complexity:
O(n)
- Space complexity:
O(1)
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
for(int i=1;i<cost.length;i++){
if(cost[i] > cost[i-1] ){
cost[i]=cost[i-1];
}
}
return cost;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | beats 100% simple java solution | beats-100-simple-java-solution-by-vikasn-ggqk | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | vikasND | NORMAL | 2025-04-09T04:28:23.753688+00:00 | 2025-04-09T04:28:23.753688+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
for(int i=1;i<cost.length;i++){
if(cost[i]>cost[i-1]){
cost[i]=cost[i-1];
}
}
return cost;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Beats 100% Users || Prefix Sum Concept || Minimum Value | beats-100-users-prefix-sum-concept-minim-3l24 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | devansh_dubey | NORMAL | 2025-04-08T13:01:31.720590+00:00 | 2025-04-08T13:01:31.720590+00:00 | 1 | false | 
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int mini = cost[0];
for(int i = 1;i<cost.size();i++){
mini = min(cost[i],mini);
cost[i] = mini;
}
return cost;
}
};
``` | 0 | 0 | ['Array', 'Prefix Sum', 'C++'] | 0 |
minimum-cost-to-reach-every-position | Swift💯 1 liner; Ridiculously tiny! | swift-1-liner-ridiculously-tiny-by-upvot-v0ic | One-Liner, terse (accepted answer) | UpvoteThisPls | NORMAL | 2025-04-08T06:30:06.675057+00:00 | 2025-04-08T06:30:06.675057+00:00 | 1 | false | **One-Liner, terse (accepted answer)**
```
class Solution {
func minCosts(_ cost: [Int]) -> [Int] {
cost.reductions(min)
}
}
``` | 0 | 0 | ['Swift'] | 0 |
minimum-cost-to-reach-every-position | 100% easy solution | 100-easy-solution-by-m_beenu_sree-go9j | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | m_beenu_sree | NORMAL | 2025-04-07T16:12:07.891366+00:00 | 2025-04-07T16:12:07.891366+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
asn=[]
m=cost[0]
for a in cost:
m=min(a,m)
asn.append(m)
return asn
``` | 0 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | C++ solution | 0ms | c-solution-0ms-by-nguyenchiemminhvu-88l3 | null | nguyenchiemminhvu | NORMAL | 2025-04-07T12:14:38.266268+00:00 | 2025-04-07T12:14:38.266268+00:00 | 1 | false | ```
class Solution
{
public:
vector<int> minCosts(vector<int>& cost)
{
std::vector<int> res(cost.size(), 0);
std::stack<int> st;
for (int i = 0; i < cost.size(); i++)
{
if (st.empty())
{
st.push(i);
}
else
{
if (cost[i] < cost[st.top()])
{
st.push(i);
}
}
}
int right = cost.size();
while (!st.empty())
{
int left = st.top();
st.pop();
for (int i = left; i < right; i++)
{
res[i] = cost[left];
}
right = left;
}
return res;
}
};
``` | 0 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | Easy pease lemon squeezy!!! | easy-pease-lemon-squeezy-by-vihaanx001-ry0g | IntuitionThe problem seems to involve adjusting a list of costs to ensure that no subsequent cost is greater than any previous cost. The goal is to traverse the | Vihaanx001 | NORMAL | 2025-04-07T03:28:16.309108+00:00 | 2025-04-07T03:28:16.309108+00:00 | 1 | false | # Intuition
The problem seems to involve adjusting a list of costs to ensure that no subsequent cost is greater than any previous cost. The goal is to traverse the list and modify each element if it's greater than the element before it.
# Approach
The function iterates through the given list of costs. For each element starting from the second one, it compares it to the previous element. If the current element is greater than the one before it, the function sets the current element to the value of the previous element, effectively ensuring no increase in subsequent values.
# Complexity
- Time complexity:
The time complexity is $$O(n)$$ since we iterate through the list once, where `n` is the number of elements in the list.
- Space complexity:
The space complexity is $$O(1)$$ as the modifications are done in-place and no additional space is required beyond the input list.
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
for(int i =0; i<cost.size()-1; i++){
if(cost[i+1]>cost[i]){
cost[i+1]=cost[i];
}
}
return cost;
}
};
``` | 0 | 0 | ['Array', 'C++'] | 0 |
minimum-cost-to-reach-every-position | Simple solution | simple-solution-by-sachinab-kbn0 | Code | sachinab | NORMAL | 2025-04-06T08:20:26.501079+00:00 | 2025-04-06T08:20:26.501079+00:00 | 4 | false |
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int[] res = new int[cost.length];
int min = cost[0];
for(int c=0; c<cost.length; c++){
min = Math.min(min, cost[c]);
res[c]=min;
}
return res;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Simple | simple-by-meky20500-0whh | Code | meky20500 | NORMAL | 2025-04-05T21:46:17.404809+00:00 | 2025-04-05T21:46:17.404809+00:00 | 2 | false | # Code
```csharp []
public class Solution
{
public int[] MinCosts(int[] cost)
{
int min = int.MaxValue;
for(int i = 0; i < cost.Length; i++)
{
cost[i] = Math.Min(cost[i],min);
min = Math.Min(cost[i],min);
}
return cost;
}
}
``` | 0 | 0 | ['Array', 'C#'] | 0 |
minimum-cost-to-reach-every-position | clear logic | clear-logic-by-sophie84-ktqx | Intuition"move forward in the line" == "move backward in the index, starting from 0"Code | sophie84 | NORMAL | 2025-04-05T20:11:25.582066+00:00 | 2025-04-05T20:11:45.712307+00:00 | 2 | false | # Intuition
"move forward in the line" == "move backward in the index, starting from 0"
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
n = len(cost)
ans = []
ans.append(cost[0])
for i in range(1, n):
ans_min = min(ans[i-1], cost[i])
ans.append(ans_min)
return ans
``` | 0 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | ✅ Easy Approach || Beats 100% || Beginner Friendly ✅ | easy-approach-beats-100-beginner-friendl-3ml0 | IntuitionApproachComplexity
Time complexity: O(n)
Space complexity: O(n)
Code | sejalpuraswani | NORMAL | 2025-04-05T15:01:23.117711+00:00 | 2025-04-05T15:01:23.117711+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```javascript []
/**
* @param {number[]} cost
* @return {number[]}
*/
var minCosts = function(cost) {
let answer = [];
answer.push(cost[0]);
for(let i=1;i<cost.length;i++){
answer.push(Math.min(answer[answer.length-1],cost[i]));
}
return answer;
};
``` | 0 | 0 | ['JavaScript'] | 0 |
minimum-cost-to-reach-every-position | easy loop | easy-loop-by-julia_2-m72c | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | julia_2 | NORMAL | 2025-04-05T11:52:28.551250+00:00 | 2025-04-05T11:52:28.551250+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
ans = [cost[0]]
for i in range(1,len(cost)):
if cost[i] < ans[-1]:
ans.append(cost[i])
else:
ans.append(ans[-1])
return ans
``` | 0 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | [C++] Previous smaller. O(n). | c-previous-smaller-on-by-lovebaonvwu-cuyh | null | lovebaonvwu | NORMAL | 2025-04-05T07:20:36.262098+00:00 | 2025-04-05T07:22:24.291977+00:00 | 3 | false |
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n = cost.size();
vector<int> ans(n);
for (int i = 0, prev = INT_MAX; i < n; ++i) {
ans[i] = min(prev, cost[i]);
prev = min(prev, cost[i]);
}
return ans;
}
};
``` | 0 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | Easy solution | easy-solution-by-angielf-0eos | Approach
Create an array answer of the same size as cost to store the minimum costs.
Base Case: The cost to reach the first position (index 0) is simply the | angielf | NORMAL | 2025-04-05T06:55:36.277541+00:00 | 2025-04-05T06:55:36.277541+00:00 | 1 | false | # Approach
1. Create an array answer of the same size as cost to store the minimum costs.
2. Base Case: The cost to reach the first position (index 0) is simply the cost to swap with the first person.
3. Iterate through the array: For each position i from 1 to n-1, we calculate the minimum cost to reach that position:
- We can either swap with the person directly in front of us (at position i) or we can come from the previous position (i-1) and swap with the person there.
4. Return the answer array which contains the minimum costs for each position.
# Complexity
- Time complexity:
$$O(n)$$, where n is the length of the cost array. This is because we iterate through the array exactly once to compute the minimum costs for each position.
- Space complexity:
$$O(n)$$ due to the answer array that we create to store the minimum costs for each position.
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
n = len(cost)
answer = [0] * n
# The cost to reach the first position (index 0)
answer[0] = cost[0]
for i in range(1, n):
# The cost to reach position i is the minimum of:
# 1. The cost to reach position i-1 + the cost to swap with person i-1
# 2. The cost to swap directly with person i
answer[i] = min(answer[i - 1], cost[i])
return answer
```
``` javascript []
/**
* @param {number[]} cost
* @return {number[]}
*/
var minCosts = function(cost) {
const n = cost.length;
const answer = new Array(n).fill(0);
// The cost to reach the first position (index 0)
answer[0] = cost[0];
for (let i = 1; i < n; i++) {
// The cost to reach position i is the minimum of:
// 1. The cost to reach position i-1 plus the cost to swap with person i-1
// 2. The cost to swap directly with person i
answer[i] = Math.min(answer[i - 1], cost[i]);
}
return answer;
};
```
``` php []
class Solution {
/**
* @param Integer[] $cost
* @return Integer[]
*/
function minCosts($cost) {
$n = count($cost);
$answer = array_fill(0, $n, 0);
// The cost to reach the first position (index 0)
$answer[0] = $cost[0];
for ($i = 1; $i < $n; $i++) {
// The cost to reach position i is the minimum of:
// 1. The cost to reach position i-1 plus the cost to swap with person i-1
// 2. The cost to swap directly with person i
$answer[$i] = min($answer[$i - 1], $cost[$i]);
}
return $answer;
}
}
``` | 0 | 0 | ['Array', 'PHP', 'Python3', 'JavaScript'] | 0 |
minimum-cost-to-reach-every-position | Beats 100% | O(n) trivial solution | beats-100-on-trivial-solution-by-shoryas-4po2 | IntuitionSo whenever a minimum cost i exists in front of current location, go there, in this cost incurred to me will be cost[i] and we need to travel to indexe | shoryasethia | NORMAL | 2025-04-04T22:57:00.215353+00:00 | 2025-04-04T22:57:00.215353+00:00 | 3 | false | # Intuition
```Rules []
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.
```
So whenever a minimum cost `i` exists in front of `current location`, go there, in this cost incurred to me will be `cost[i]` and we need to travel to indexes lying between `current location` and `i` we can go for free backwards.
# Approach
```Pseudocode []
minm = infinity
for i in 0...N:
minm = min(cost[i],minm)
answer[i] = minm
return answer
```
# Complexity
- Time complexity:
O(N)
- Space complexity:
O(1), answer/result space is excluded.
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
vector<int> result;
int minm = INT_MAX;
for(int i=0;i<cost.size();i++){
minm = min(minm,cost[i]);
result.push_back(minm);
}
return result;
}
};
``` | 0 | 0 | ['Array', 'C++'] | 0 |
minimum-cost-to-reach-every-position | 3502. Minimum Cost to Reach Every Position | 3502-minimum-cost-to-reach-every-positio-h06c | IntuitionEasy one! Initially, the problem looked little bit difficult to solve optimally. But, when deeply looking on it, I found that it can be solved using Mi | SPD-LEGEND | NORMAL | 2025-04-04T17:12:02.402101+00:00 | 2025-04-04T17:12:02.402101+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Easy one! Initially, the problem looked little bit difficult to solve optimally. But, when deeply looking on it, I found that it can be solved using Minimum Prefix Array!
# Approach
<!-- Describe your approach to solving the problem. -->
Used Minimum Prefix Array!
# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
for(int i=1, min = cost[0];i<cost.length;++i)
cost[i] = Math.min(cost[i],cost[i-1]);
return cost;
}
}
``` | 0 | 0 | ['Array', 'Java'] | 0 |
minimum-cost-to-reach-every-position | 3502. Minimum Cost to Reach Every Position | 3502-minimum-cost-to-reach-every-positio-yoq5 | IntuitionEasy one! Initially, the problem looked little bit difficult to solve optimally. But, when deeply looking on it, I found that it can be solved using Mi | SPD-LEGEND | NORMAL | 2025-04-04T17:11:58.587250+00:00 | 2025-04-04T17:11:58.587250+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Easy one! Initially, the problem looked little bit difficult to solve optimally. But, when deeply looking on it, I found that it can be solved using Minimum Prefix Array!
# Approach
<!-- Describe your approach to solving the problem. -->
Used Minimum Prefix Array!
# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
for(int i=1, min = cost[0];i<cost.length;++i)
cost[i] = Math.min(cost[i],cost[i-1]);
return cost;
}
}
``` | 0 | 0 | ['Array', 'Java'] | 0 |
minimum-cost-to-reach-every-position | gaze | minimum-cost-to-reach-every-position-by-804l4 | IntuitionReturning the newly created array(list)ApproachEven we are at last so anyhow we have to pay for the last second person , than if next person has bribe | Tejas_Gowda_6 | NORMAL | 2025-04-04T16:17:10.406842+00:00 | 2025-04-04T16:34:38.784799+00:00 | 1 | false | # Intuition
Returning the newly created array(list)
# Approach
Even we are at last so anyhow we have to pay for the last second person , than if next person has bribe over then what we paid before ,just replace with respective price of him/her ,else retain the same money what we paid last highest.
# Complexity
- Time complexity:
many cases took 0 m/s
- Space complexity:
45 percent approximately
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
l = []
n=cost[0]
for i in range(1,len(cost)):
l.append(n)
if cost[i]<n:
n=cost[i]
l.append(n)
return l
``` | 0 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | Simple Java solution | simple-java-solution-by-rohanaggarwal232-r13f | IntuitionKeep track of the minimum value that you encounter while traversing,
if the next value is bigger than the minValue then use minValue else set minValue | rohanaggarwal232 | NORMAL | 2025-04-04T10:57:39.274361+00:00 | 2025-04-04T10:57:39.274361+00:00 | 2 | false | # Intuition
Keep track of the minimum value that you encounter while traversing,
if the next value is bigger than the minValue then use minValue else set minValue equal to the current value and set it
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int[] result = new int[cost.length];
int minValue = Integer.MAX_VALUE;
for(int i=0;i<cost.length;i++) {
if(cost[i] < minValue) {
minValue = cost[i];
}
result[i] = minValue;
}
return result;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Python Solution || In-space Monotonic stack. | python-solution-in-space-monotonic-stack-mdff | Code | SEAQEN_KANI | NORMAL | 2025-04-04T08:13:34.396302+00:00 | 2025-04-04T08:13:34.396302+00:00 | 4 | false | # Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
for i in range(1, len(cost)):
cost[i] = min(cost[i], cost[i-1])
return cost
``` | 0 | 0 | ['Array', 'Python3'] | 0 |
minimum-cost-to-reach-every-position | Optimized simple solution - beats 97.15%🔥 | optimized-simple-solution-beats-9715-by-c1sli | Complexity
Time complexity: O(N)
Space complexity: O(1)
Code | cyrusjetson | NORMAL | 2025-04-04T07:18:35.789349+00:00 | 2025-04-04T07:18:35.789349+00:00 | 1 | false | # Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int prev = cost[0];
for (int i = 1; i < cost.length; i++) {
if (cost[i] > prev) cost[i] = prev;
else prev = cost[i];
}
return cost;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Keeping track of the most recent minimum value encountered so far | keeping-track-of-the-most-recent-minimum-2g7m | IntuitionWe'll keep track of the current "most recent" minimum value and assign it to each value in cost until we encounter the next minimum value.ApproachThe p | mnjk | NORMAL | 2025-04-03T20:20:39.448511+00:00 | 2025-04-03T20:20:39.448511+00:00 | 4 | false | # Intuition
We'll keep track of the current "most recent" minimum value and assign it to each value in `cost` until we encounter the next minimum value.
# Approach
The problem boils down to simply keeping track of the *most recent minimum value* during stepping from left to right. At each step, we'll check whether we found a new minimum value and update our tracking variable. We simply assign the current minimum value to the current position in `cost`.
# Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(1)$$
# Code
```typescript []
function minCosts(cost: number[]): number[] {
let min = Infinity;
for (let i = 0; i < cost.length; i++) {
min = Math.min(min, cost[i]);
cost[i] = min;
}
return cost;
};
``` | 0 | 0 | ['Array', 'TypeScript'] | 0 |
minimum-cost-to-reach-every-position | Easiest and simply understandable JAVA solution | Beats 100% | easiest-and-simply-understandable-java-s-vbwn | IntuitionTo use optimized solution in terms of space and time complexity.ApproachCompare current element with upcoming element and if upcoming element less then | tejasagashe | NORMAL | 2025-04-03T19:45:24.202432+00:00 | 2025-04-03T19:45:24.202432+00:00 | 2 | false | # Intuition
To use optimized solution in terms of space and time complexity.
# Approach
Compare current element with upcoming element and if upcoming element less then swap it with the current one.
# Complexity
- Time complexity:
O(n)
- Space complexity:
O(1)
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
for(int i=0; i<cost.length-1;i++){
if(cost[i]<cost[i+1]){
cost[i+1] = cost[i];
}
}
return cost;
}
}
``` | 0 | 0 | ['Array', 'Java'] | 0 |
minimum-cost-to-reach-every-position | python | python-by-binkswang-kh0k | Code | BinksWang | NORMAL | 2025-04-03T18:53:49.398219+00:00 | 2025-04-03T18:53:49.398219+00:00 | 1 | false | # Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
min = cost[0]
for i in range (0, len(cost)):
if cost[i] >= min:
cost[i] = min
else:
min = cost[i]
return cost
``` | 0 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | c# | c-by-binkswang-nbgk | Code | BinksWang | NORMAL | 2025-04-03T18:50:54.226485+00:00 | 2025-04-03T18:50:54.226485+00:00 | 4 | false | # Code
```csharp []
public class Solution {
public int[] MinCosts(int[] cost) {
var min = cost[0];
for(var i = 1; i < cost.Length; i++){
if(cost[i] >= min)
cost[i] = min;
else
min = cost[i];
}
return cost;
}
}
``` | 0 | 0 | ['C#'] | 0 |
minimum-cost-to-reach-every-position | o(n) space and o(1) space complexity | on-space-and-o1-space-complexity-by-arfa-gc2r | ApproachComplexity
Time complexity:
O(n) space complexity
Space complexity:
O(1) space complexity
Code | ARFATHkhan | NORMAL | 2025-04-03T17:15:10.029493+00:00 | 2025-04-03T17:15:10.029493+00:00 | 1 | false |
# Approach
1. create a price as variable and assign cost of index-0.
---
int price= cost[0];
```
2. iterate through the array.
3. check if price < cost then change the price with cost.
4. cost is assign to price
```
```
cost[i]=price;
```
# Complexity
- Time complexity:
O(n) space complexity
- Space complexity:
O(1) space complexity
# Code
```java []
class Solution {
public int[] minCosts(int[] cost)
{
int price=cost[0];
for(int i=1;i<cost.length;i++)
{
if(cost[i]<price)
{
price=cost[i];
}
cost[i]=price;
}
return cost;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Simple C++ solution by traversing the array. | simple-c-solution-by-traversing-the-arra-0j9k | Complexity
Time complexity:O(N)
Space complexity:O(1)
Code | vansh16 | NORMAL | 2025-04-03T07:20:29.732402+00:00 | 2025-04-03T07:20:29.732402+00:00 | 2 | false |
# Complexity
- Time complexity:O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
vector<int> ans(cost.size(),0);
ans[0]=cost[0];
for(int i=1;i<cost.size();i++)
ans[i]=min(cost[i],ans[i-1]);
return ans;
}
};
``` | 0 | 0 | ['Array', 'C++'] | 0 |
minimum-cost-to-reach-every-position | 1 ms Beats 97.36% | 1-ms-beats-9736-by-iamsd-uf9v | Code | iamsd_ | NORMAL | 2025-04-03T05:48:00.359380+00:00 | 2025-04-03T05:48:00.359380+00:00 | 3 | false |
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int len = cost.length;
int[] ans = new int[len];
for (int i = 0; i < len; i++) {
ans[i] = cost[i];
if(i > 0) ans[i] = Math.min(ans[i], ans[i -1]);
}
return ans;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Minimum Cost to Reach Every Position | minimum-cost-to-reach-every-position-by-41cvp | IntuitionEvery time you enter the next step, you want the minimum and save that until you find a new minimum. Repeat till the end of the arrayComplexity
Time co | Ayaanmk2004 | NORMAL | 2025-04-03T01:24:59.108211+00:00 | 2025-04-03T01:24:59.108211+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Every time you enter the next step, you want the minimum and save that until you find a new minimum. Repeat till the end of the array
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Go through the array once
O(n)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1)
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
mini = cost[0]
for i in range(len(cost)):
if(cost[i] < mini):
mini = cost[i]
else:
cost[i] = mini
return cost
``` | 0 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | Python O(n) Time, O(1) Space | python-on-time-o1-space-by-johnmullan-97hl | Complexity
Time complexity: O(n)
Space complexity: O(1)
Code | JohnMullan | NORMAL | 2025-04-02T22:48:33.768470+00:00 | 2025-04-02T22:48:33.768470+00:00 | 8 | false | # Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python []
class Solution(object):
def minCosts(self, cost):
"""
:type cost: List[int]
:rtype: List[int]
"""
m=float("inf")
for idx in range(len(cost)):
m=min(m,cost[idx])
cost[idx]=m
return cost
``` | 0 | 0 | ['Python'] | 0 |
minimum-cost-to-reach-every-position | Solution for beginner | solution-for-beginner-by-nitin_dumka11-ldy0 | IntuitionApproachComplexity
Time complexity:
The algorithm iterates through the cost array once, processing each element in O(1) time. Since there is only a sin | nitin_dumka11 | NORMAL | 2025-04-02T17:45:47.273900+00:00 | 2025-04-02T17:45:47.273900+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->The problem requires us to keep track of the minimum cost encountered so far as we move through the array. To achieve this, we initialize an answer array where the first element remains the same as in the given cost array. Then, for each subsequent element, we compare it with the previously stored minimum value. If the previous minimum is smaller, we carry it forward; otherwise, we update it with the new cost. This ensures that at every index, we store the smallest cost encountered from the start up to that position. By the end of the loop, the answer array contains the minimum cost seen so far at each step, providing an efficient way to track the lowest values as we traverse the array.
# Approach
<!-- Describe your approach to solving the problem. -->We start by initializing an answer array of the same size as cost, where answer[0] is set to cost[0] since the first element remains unchanged. Then, we iterate through the cost array from index 1 to n-1. At each step, we compare the current cost with the minimum value encountered so far (stored in answer[i-1]). If the previous minimum is smaller, we carry it forward; otherwise, we update it with the new cost. This way, answer[i] always holds the minimum cost seen up to that index. Finally, we return the answer array as the result. This approach ensures we efficiently compute the required values in a single pass with a time complexity of O(n).
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
The algorithm iterates through the cost array once, processing each element in O(1) time. Since there is only a single loop running from 0 to n-1, the overall time complexity is O(n).
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->We use an additional array answer of size n to store the results. Apart from the input array, this requires O(n) extra space. Therefore, the space complexity is O(n).
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int n=cost.length;
int[] answer=new int[n];
answer[0]=cost[0];
for(int i=1;i<n;i++){
if(answer[i-1]<cost[i]){
answer[i]=answer[i-1];
} else {
answer[i]=cost[i];
}
}
return answer;
}
} | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Python o(n) | python-on-by-navani_hk-bcss | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | navani_hk | NORMAL | 2025-04-02T09:52:50.473810+00:00 | 2025-04-02T09:52:50.473810+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python []
class Solution(object):
def minCosts(self, cost):
"""
:type cost: List[int]
:rtype: List[int]
"""
if(len(cost)==0):
return []
if(cost[0]==1):
return [1]*len(cost)
ret=[]
ret.append(cost[0])
min=cost[0]
for i in range(1,len(cost)):
if min<cost[i]:
ret.append(min)
else:
min=cost[i]
ret.append(min)
return ret
``` | 0 | 0 | ['Python'] | 0 |
minimum-cost-to-reach-every-position | Best Solution | best-solution-by-vemohan15-vqvf | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | vemohan15 | NORMAL | 2025-04-02T08:00:23.680347+00:00 | 2025-04-02T08:00:23.680347+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```csharp []
public class Solution {
public int[] MinCosts(int[] cost) {
List<int> a=new List<int>();
int min=999;
for(int i=0;i<cost.Length;i++)
{
min=Math.Min(min,cost[i]);
a.Add(min);
}
return a.ToArray();
}
}
``` | 0 | 0 | ['C#'] | 0 |
minimum-cost-to-reach-every-position | easy peasy | easy-peasy-by-aditya821-fjal | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | aditya821 | NORMAL | 2025-04-01T19:52:21.823094+00:00 | 2025-04-01T19:52:21.823094+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n=*max_element(cost.begin(),cost.end());
int sz=cost.size();
vector<int>v;
for(int i=0;i<sz;i++){
if(n>cost[i]){
v.push_back(cost[i]);
n=cost[i];
}else {
v.push_back(n);
}
}
return v;
}
};
``` | 0 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | Linear approach solution | linear-approach-solution-by-abhay_mandal-mngp | null | Abhay_mandal | NORMAL | 2025-04-01T09:27:30.708452+00:00 | 2025-04-01T09:27:30.708452+00:00 | 2 | false | ```
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
vector<int> ans;
int mini=INT_MAX;
for(int i=0; i<cost.size(); i++){
if(cost[i]<mini){
ans.push_back(cost[i]);
mini = cost[i];
}
else ans.push_back(mini);
}
return ans;
}
};
``` | 0 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | Simple Swift Solution | simple-swift-solution-by-felisviridis-6grr | Code | Felisviridis | NORMAL | 2025-04-01T07:05:47.019979+00:00 | 2025-04-01T07:05:47.019979+00:00 | 1 | false | 
# Code
```swift []
class Solution {
func minCosts(_ cost: [Int]) -> [Int] {
var minCost = cost[0], res = [Int]()
for i in 0..<cost.count {
minCost = min(minCost, cost[i])
res.append(minCost)
}
return res
}
}
``` | 0 | 0 | ['Swift'] | 0 |
minimum-cost-to-reach-every-position | Java easy solution | java-easy-solution-by-bat5738-puii | Code | bat5738 | NORMAL | 2025-04-01T06:44:24.249997+00:00 | 2025-04-01T06:44:24.249997+00:00 | 1 | false |
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
var prevMin = Integer.MAX_VALUE;
for (int i = 0; i < cost.length; i++) {
var current = cost[i];
if(current < prevMin){
prevMin = current;
} else {
cost[i] = prevMin;
}
}
return cost;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Simple Code - Beats 100%, Time complexity O(n), without any extra space | simple-code-beats-100-time-complexity-on-8hss | Code | Anjan_B | NORMAL | 2025-04-01T05:43:12.349427+00:00 | 2025-04-01T05:43:12.349427+00:00 | 4 | false |
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
for(int i=1;i<cost.length;i++)
{
if(cost[i-1]<cost[i])
{
cost[i] = cost[i-1];
}
}
return cost;
}
}
``` | 0 | 0 | ['Array', 'Java'] | 0 |
minimum-cost-to-reach-every-position | Simple solution | simple-solution-by-gv1506-4osy | IntuitionApproachComplexity
Time complexity: O(n)
Space complexity: O(n)
Code | gv1506 | NORMAL | 2025-04-01T05:00:29.678260+00:00 | 2025-04-01T05:00:29.678260+00:00 | 5 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```swift []
class Solution {
func minCosts(_ cost: [Int]) -> [Int] {
let n = cost.count
var ans = Array(repeating: 0, count: n)
var minSofar = cost[0]
for i in 0..<n {
minSofar = min(minSofar, cost[i])
ans[i] = minSofar
}
return ans
}
}
``` | 0 | 0 | ['Swift'] | 0 |
minimum-cost-to-reach-every-position | Simple C Solution with Greedy Approach | simple-c-solution-with-greedy-approach-b-p4va | Complexity
Time complexity:
O(N)
Space complexity:
O(N)Code | kavyachetwani | NORMAL | 2025-03-31T19:58:10.150065+00:00 | 2025-03-31T19:58:10.150065+00:00 | 12 | false |
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$$O(N)$$
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$$O(N)$$
# Code
```c []
int* minCosts(int* cost, int costSize, int* returnSize) {
*returnSize = costSize;
int* answer = (int*)malloc(costSize * sizeof(int));
if (!answer) return NULL;
int minCost = cost[0];
answer[0] = minCost;
for (int i = 1; i < costSize; i++) {
if (cost[i] < minCost) {
minCost = cost[i];
}
answer[i] = minCost;
}
return answer;
}
``` | 0 | 0 | ['C'] | 0 |
minimum-cost-to-reach-every-position | The fastest possible solution 🚀 | the-fastest-possible-solution-by-3head-mp0g | Complexity
Time complexity: O(n)
Space complexity: O(1)
Code | 3Head | NORMAL | 2025-03-31T18:49:13.486132+00:00 | 2025-03-31T18:49:13.486132+00:00 | 10 | false | # Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```typescript []
function minCosts(cost: number[]): number[] {
let minCost = cost[0];
for (let index = 1; index < cost.length; index++) {
if (cost[index] < minCost) minCost = cost[index];
cost[index] = minCost;
}
return cost;
};
```
| 0 | 0 | ['TypeScript', 'JavaScript'] | 0 |
minimum-cost-to-reach-every-position | Java Solution Beats 100% | java-solution-beats-100-by-ashwinkumar02-f09i | IntuitionApproachComplexity
Time complexity: O(N)
Space complexity: O(1)
Code | ashwinkumar021 | NORMAL | 2025-03-31T18:06:51.774589+00:00 | 2025-03-31T18:06:51.774589+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
for(int i=1;i<cost.length;i++){
if(cost[i-1]<cost[i])
cost[i]=cost[i-1];
}
return cost;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | C# Solution | c-solution-by-user1624vy-eegq | Complexity
Time complexity: O(n)
Space complexity: O(n)
Code | user1624VY | NORMAL | 2025-03-31T17:50:27.758549+00:00 | 2025-03-31T17:50:27.758549+00:00 | 6 | false | # Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(n)$$
# Code
```csharp []
public class Solution {
public int[] MinCosts(int[] cost)
{
int[] res = new int[cost.Length];
int min = int.MaxValue;
for (int i = 0; i < cost.Length; ++i)
{
if (cost[i] < min)
{
min = cost[i];
}
res[i] = min;
}
return res;
}
}
``` | 0 | 0 | ['C#'] | 0 |
minimum-cost-to-reach-every-position | Track Pre-Min | track-pre-min-by-linda2024-gtsu | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | linda2024 | NORMAL | 2025-03-31T16:56:47.184074+00:00 | 2025-03-31T16:56:47.184074+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```csharp []
public class Solution {
public int[] MinCosts(int[] cost) {
int len = cost.Length;
int[] preMin = new int[len];
preMin[0] = cost[0];
for(int i = 1; i < len; i++)
{
preMin[i] = Math.Min(cost[i], preMin[i-1]);
}
return preMin;
}
}
``` | 0 | 0 | ['C#'] | 0 |
minimum-cost-to-reach-every-position | Beats 100% Runtime Solution in Java | beats-100-runtime-solution-in-java-by-sh-mbag | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | ShDey7986 | NORMAL | 2025-03-31T16:10:58.092427+00:00 | 2025-03-31T16:10:58.092427+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int n=cost.length;
int min=cost[0];
int[] arr=new int[n];
arr[0]=cost[0];
for(int i=1;i<n;i++){
arr[i]=Math.min(cost[i],min);
min=Math.min(arr[i],min);
}
return arr;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Easy Python3 Solution | easy-python3-solution-by-ompimple26-uyyw | IntuitionThe problem requires us to maintain the minimum value seen so far as we iterate through the given list of costs. This means that at each index, we shou | OmPimple26 | NORMAL | 2025-03-31T15:46:52.348834+00:00 | 2025-03-31T15:46:52.348834+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to maintain the minimum value seen so far as we iterate through the given list of costs. This means that at each index, we should store the smallest value encountered up to that point.
# Approach
<!-- Describe your approach to solving the problem. -->
* We use Python's itertools.accumulate() function with min as the accumulation function.
* accumulate(cost, min) ensures that for each index i, the value stored is min(cost[0], cost[1], ..., cost[i]).
* This effectively creates a running minimum list, where each element represents the smallest cost encountered so far.
# Complexity
- Time complexity: O(n) — We iterate through the list once
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(n) — Since we return a new list of size
𝑛.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
return list(accumulate(cost, min))
``` | 0 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | Minimum Cost to Reach Every Position | minimum-cost-to-reach-every-position-by-1i8dq | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | uongsuadaubung | NORMAL | 2025-03-31T14:56:47.041862+00:00 | 2025-03-31T14:56:47.041862+00:00 | 6 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```typescript []
function minCosts(cost: number[]): number[] {
const n = cost.length;
const answer = new Array(n).fill(0);
let minCost = Infinity; // Lưu chi phí nhỏ nhất đã gặp
for (let i = 0; i < n; i++) {
minCost = Math.min(minCost, cost[i]); // Cập nhật chi phí nhỏ nhất
answer[i] = minCost; // Chi phí tối thiểu để đến vị trí i
}
return answer;
};
``` | 0 | 0 | ['TypeScript'] | 0 |
minimum-cost-to-reach-every-position | [C++] Minimum Number Encountered | c-minimum-number-encountered-by-amanmeha-dvhq | Complexity
Time complexity: O(n)
Space complexity: O(n)
Code | amanmehara | NORMAL | 2025-03-31T14:13:07.978132+00:00 | 2025-03-31T14:13:07.978132+00:00 | 3 | false | # Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(n)$$
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int n = cost.size();
int minimum = cost[0];
for (int i = 0; i < n; i++) {
if (cost[i] < minimum) {
minimum = cost[i];
} else {
cost[i] = minimum;
}
}
return cost;
}
};
``` | 0 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | Ruby one-liner, beats 100%/100% | ruby-one-liner-beats-100100-by-dnnx-off4 | null | dnnx | NORMAL | 2025-03-31T08:54:57.915014+00:00 | 2025-03-31T08:54:57.915014+00:00 | 3 | false | ```ruby []
# @param {Integer[]} cost
# @return {Integer[]}
def min_costs(cost)
999.yield_self { |x| cost.map { x = [x, _1].min } }
end
``` | 0 | 0 | ['Ruby'] | 0 |
minimum-cost-to-reach-every-position | Straight forward solution [Java] | straight-forward-solutions-java-by-mrpon-3ppx | IntuitionThe goal of this problem is to modify the given array cost such that each element is replaced with the minimum cost seen so far from the left. This ens | mrpong | NORMAL | 2025-03-31T06:47:03.192427+00:00 | 2025-03-31T06:47:52.765302+00:00 | 4 | false | # Intuition
The goal of this problem is to modify the given array cost such that each element is replaced with the minimum cost seen so far from the left. This ensures that each element at index i is at most the cost of the previous element.
# Approach
We iterate through the array from left to right. At each index i, we compare cost[i] with the previous element cost[i - 1]. If cost[i] is greater than cost[i - 1], we update cost[i] to be cost[i - 1]. This effectively ensures that each element is non-increasing with respect to the left side of the array.
# Complexity
- Time complexity:
$$O(n)$$
- Space complexity:
$$O(1)$$
# Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
for (int i = 1;i < cost.length; ++i) {
int p = i - 1;
if (cost[i] > cost[p]) {
cost[i] = cost[p];
}
}
return cost;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | beats 100%||java implementation | beats-100java-implementation-by-_anuj_sh-dnt1 | Code | _anuj_shandilya | NORMAL | 2025-03-31T06:25:04.825765+00:00 | 2025-03-31T06:25:04.825765+00:00 | 2 | false | # Code
```java []
class Solution {
public int[] minCosts(int[] cost) {
int n=cost.length;
int[] ans=new int[n];
ans[0]=cost[0];
for(int i=1;i<n;i++){
ans[i]=Math.min(ans[i-1],cost[i]);
}
return ans;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | Not sure if this is correct but it worked, looking for feedback if anyone has any | not-sure-if-this-is-correct-but-it-worke-hppi | IntuitionI noticed a pattern in the output where local minimum was always the value, and hence I just kept track of the local min in each iteration, and appened | adilkhan510 | NORMAL | 2025-03-31T05:10:21.130509+00:00 | 2025-03-31T05:10:21.130509+00:00 | 5 | false | # Intuition
I noticed a pattern in the output where local minimum was always the value, and hence I just kept track of the local min in each iteration, and appeneded it to the output.
# Approach
# Complexity
- Time complexity:
$$O(n)$$
- Space complexity:
- $$O(n)$$
# Code
```python []
class Solution(object):
def minCosts(self, cost):
"""
:type cost: List[int]
:rtype: List[int]
"""
curr_low = cost[0]
output = []
for i in range(0,len(cost)):
curr_low = min(curr_low,cost[i])
output.append(curr_low)
return output
``` | 0 | 0 | ['Python'] | 0 |
minimum-cost-to-reach-every-position | Java 100% | java-100-by-jaidevramakrishna-vldm | IntuitionThis is just creating a min cost array.ApproachWe have the following input:
Let A[I+]:∣A∣=N
Let Ai∈A:0⩽i<N
Let R[I+]:∣R∣=N
Let R[0]=A[0]
Then ∀Ri:1⩽i | jaidevramakrishna | NORMAL | 2025-03-31T02:18:05.177718+00:00 | 2025-03-31T02:18:05.177718+00:00 | 3 | false | # Intuition
This is just creating a min cost array.
# Approach
We have the following input:
- Let $A[I^+] : |A|=N$
- Let $A_i \in A : 0 \leqslant i <N$
Let $R[I^+] : |R|=N$
Let $R[0] = A[0]$
Then $\forall R_i : 1 \leqslant i < N \to R_i = min(A_i,R_{i-1})$
# Complexity
- Time complexity: $O(N)$
- Space complexity: $O(N)$
# Code
```java []
class Solution
{
public int[] minCosts(int[] cost)
{
int l = cost.length;
int [] r = new int[l];
r[0] = cost[0];
for(int i=1; i<l; i++)
r[i] = Math.min(cost[i],r[i-1]);
return r;
}
}
``` | 0 | 0 | ['Java'] | 0 |
minimum-cost-to-reach-every-position | update each with cur min from left | update-each-with-cur-min-from-left-by-la-9bj2 | Intuition
scan left to right
update minimum seen so far
replace each with current minimum
Code | lambdacode-dev | NORMAL | 2025-03-31T01:08:25.254166+00:00 | 2025-03-31T01:08:25.254166+00:00 | 5 | false | # Intuition
- scan left to right
- update minimum seen so far
- replace each with current minimum
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
for(int i = 1, min = cost[0]; i < cost.size(); ++i) {
min = std::min(min, cost[i]);
cost[i] = min;
}
return cost;
}
};
``` | 0 | 0 | ['C++'] | 0 |
minimum-cost-to-reach-every-position | [Python3] Good enough | python3-good-enough-by-parrotypoisson-8w99 | null | parrotypoisson | NORMAL | 2025-03-31T01:05:58.512551+00:00 | 2025-03-31T01:05:58.512551+00:00 | 3 | false | ```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
final = []
lowest = cost[0]
for i in range(len(cost)):
lowest = min(lowest, cost[i])
final.append(lowest)
return final
``` | 0 | 0 | ['Python3'] | 0 |
minimum-cost-to-reach-every-position | In-Place | in-place-by-repmop-39hq | A lot of "optimal" solutions given are using unnecessary memory and are doing unneccessary reads and copies. Only one word of memory is required for this proble | repmop | NORMAL | 2025-03-30T22:31:54.593304+00:00 | 2025-03-30T22:31:54.593304+00:00 | 2 | false | A lot of "optimal" solutions given are using unnecessary memory and are doing unneccessary reads and copies. Only one word of memory is required for this problem.
# Code
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
int m = cost[0];
for (int i = 0; i < cost.size(); i++)
{
m = min(cost[i], m);
cost[i] = m;
}
return cost;
}
};
``` | 0 | 0 | ['C++'] | 0 |
number-of-substrings-with-only-1s | [Java/C++/Python] Count | javacpython-count-by-lee215-pg9t | Explanation\ncount the current number of consecutive "1".\nFor each new element,\nthere will be more count substring,\nwith all characters 1\'s.\n\n\n## Complex | lee215 | NORMAL | 2020-07-12T04:02:13.019737+00:00 | 2020-07-13T15:40:22.812955+00:00 | 9,319 | false | ## **Explanation**\n`count` the current number of consecutive "1".\nFor each new element,\nthere will be more `count` substring,\nwith all characters 1\'s.\n<br>\n\n## **Complexity**\nTime `O(N)`\nSpace `O(1)`\n<br>\n\n**Java:**\n```java\n public int numSub(String s) {\n int res = 0, count = 0, mod = (int)1e9 + 7;\n for (int i = 0; i < s.length(); ++i) {\n count = s.charAt(i) == \'1\' ? count + 1 : 0;\n res = (res + count) % mod;\n }\n return res;\n }\n```\n\n**C++:**\n```cpp\n int numSub(string s) {\n int res = 0, count = 0, mod = 1e9 + 7;\n for (char c: s) {\n count = c == \'1\' ? count + 1 : 0;\n res = (res + count) % mod;\n }\n return res;\n }\n```\n\n**Python:**\nO(n) space though\n```py\n def numSub(self, s):\n return sum(len(a) * (len(a) + 1) / 2 for a in s.split(\'0\')) % (10**9 + 7)\n```\n | 134 | 4 | [] | 24 |
number-of-substrings-with-only-1s | [Python] Easy readable code! | python-easy-readable-code-by-akshit0699-djiv | The example would be self explanatoy:\n"0110111" can be evaluated to -->\n0120123\nNow if we sum up all these digits:\n1+2+1+2+3 = 9 is the result!\n\n\nclass S | akshit0699 | NORMAL | 2020-07-12T04:01:32.715982+00:00 | 2020-11-18T08:41:16.961783+00:00 | 2,474 | false | The example would be self explanatoy:\n"0110111" can be evaluated to -->\n0120123\nNow if we sum up all these digits:\n1+2+1+2+3 = 9 is the result!\n\n```\nclass Solution(object):\n def numSub(self, s):\n res, currsum = 0,0\n for digit in s:\n if digit == \'0\':\n currsum = 0\n else:\n currsum += 1 \n res+=currsum \n return res % (10**9+7)\n``` | 53 | 4 | ['Python'] | 5 |
number-of-substrings-with-only-1s | C++/Java sliding window | cjava-sliding-window-by-votrubac-rdja | Well, it\'s easy - \'0\' cuts the previous string. So, when we see \'0\', we calculate how many strings can be formed between i - 1 and j.\n\nThe formula is jus | votrubac | NORMAL | 2020-07-12T04:05:05.395203+00:00 | 2020-07-12T04:14:30.911357+00:00 | 3,198 | false | Well, it\'s easy - \'0\' cuts the previous string. So, when we see \'0\', we calculate how many strings can be formed between `i - 1` and `j`.\n\nThe formula is just sum of arithmetic progression (n * (n + 1) /2, or (i - j) * (i - j + 1) /2).\n\n**C++**\n```cpp\nint numSub(string s) {\n long res = 0;\n for (long i = 0, j = 0; i <= s.size(); ++i)\n if (i == s.size() || s[i] == \'0\') {\n res = (res + (i - j) * (i - j + 1) / 2) % 1000000007;\n j = i + 1;\n }\n return res;\n}\n```\n\n**Java**\n```java\npublic int numSub(String s) {\n long res = 0;\n for (long i = 0, j = 0; i <= s.length(); ++i)\n if (i == s.length() || s.charAt((int)i) == \'0\') {\n res = (res + (i - j) * (i - j + 1) / 2) % 1000000007;\n j = i + 1;\n }\n return (int)res;\n}\n``` | 39 | 1 | [] | 2 |
number-of-substrings-with-only-1s | C++ | O(n) | Easy to understand | c-on-easy-to-understand-by-di_b-1tnj | ```\n\tint numSub(string s) {\n long res = 0;\n for(int i =0; i < s.length(); ++i)\n {\n long count = 0;\n while(s[i] | di_b | NORMAL | 2020-07-12T04:04:12.995694+00:00 | 2020-07-12T04:15:04.395381+00:00 | 1,971 | false | ```\n\tint numSub(string s) {\n long res = 0;\n for(int i =0; i < s.length(); ++i)\n {\n long count = 0;\n while(s[i] == \'1\')\n count++, i++;\n res += count*(count+1)/2;\n }\n return res%1000000007;\n } | 20 | 1 | [] | 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.