8000 Add 3502_minimum_cost_to_reach_every_position · m3xw3ll/LeetCode@ad8c852 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad8c852

Browse files
committed
Add 3502_minimum_cost_to_reach_every_position
1 parent f22e0e3 commit ad8c852

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# [Minimum Cost to Reach Every Position](https://leetcode.com/problems/minimum-cost-to-reach-every-position/description/)
2+
3+
You are given an integer array cost of size n. You are currently at position n (at the end of the line) in a line of n + 1 people (numbered from 0 to n).
4+
5+
You wish to move forward in the line, but each person in front of you charges a specific amount to swap places. The cost to swap with person i is given by cost[i].
6+
7+
You are allowed to swap places with people as follows:
8+
9+
- If they are in front of you, you must pay them cost[i] to swap with them.
10+
- If they are behind you, they can swap with you for free.
11+
12+
Return an array answer of size n, where answer[i] is the minimum total cost to reach each position i in the line.
13+
14+
Example 1:
15+
```
16+
Input: cost = [5,3,4,1,3,2]
17+
18+
Output: [5,3,3,1,1,1]
19+
20+
Explanation:
21+
22+
We can get to each position in the following way:
23+
24+
i = 0. We can swap with person 0 for a cost of 5.
25+
i = 1. We can swap with person 1 for a cost of 3.
26+
i = 2. We can swap with person 1 for a cost of 3, then swap with person 2 for free.
27+
i = 3. We can swap with person 3 for a cost of 1.
28+
i = 4. We can swap with person 3 for a cost of 1, then swap with person 4 for free.
29+
i = 5. We can swap with person 3 for a cost of 1, then swap with person 5 for free.
30+
```
31+
Example 2:
32+
```
33+
Input: cost = [1,2,4,6,7]
34+
35+
Output: [1,1,1,1,1]
36+
37+
Explanation:
38+
39+
We can swap with person 0 for a cost of 1, then we will be able to reach any position i for free.
40+
```
41+
Solution
42+
```python
43+
class Solution:
44+
def minCosts(self, cost: List[int]) -> List[int]:
45+
min_prefix = [cost[0]]
46+
for i in range(1, len(cost)):
47+
min_prefix.append(min(min_prefix[-1], cost[i]))
48+
return min_prefix
49+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def min_costs(cost):
2+
min_prefix = [cost[0]]
3+
for i in range(1, len(cost)):
4+
min_prefix.append(min(min_prefix[-1], cost[i]))
5+
return min_prefix
6+
7+
8+
print(min_costs([5, 3, 4, 1, 3, 2]))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ To search for a specific problem please use ```STRG + F``` to search for.
795795
| 3483 | [Unique 3-Digit Even Numbers](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3483_unique_three_digit_even_numbers.md)
796796
| 3492 | [Maximum Containers on a Ship](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3492_maximum_containers_on_a_ship.md)
797797
| 3498 | [Reverse Degree of a String](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3498_reverse_degree_of_a_string.md)
798+
| 3502 | [Minimum Cost to Reach Every Position](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3502_minimum_cost_to_reach_every_position.md)
798799

799800

800801
### Pandas

0 commit comments

Comments
 (0)
0