3502. Minimum Cost to Reach Every Position Easy

@problem@discussion
#Array



1/**
2 * [3502] Minimum Cost to Reach Every Position
3 *
4 * <p data-end="438" data-start="104">You are given an integer array <code data-end="119" data-start="113">cost of size <code data-end="131" data-start="128">n. You are currently at position <code data-end="166" data-start="163">n (at the end of the line) in a line of <code data-end="187" data-start="180">n + 1 people (numbered from 0 to <code data-end="218" data-start="215">n).
5 * <p data-end="438" data-start="104">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 <code data-end="375" data-start="372">i is given by <code data-end="397" data-start="388">cost[i].
6 * <p data-end="487" data-start="440">You are allowed to swap places with people as follows:
7 * <ul data-end="632" data-start="488">
8 * 	<li data-end="572" data-start="488">If they are in front of you, you must pay them <code data-end="546" data-start="537">cost[i] to swap with them.
9 * 	<li data-end="632" data-start="573">If they are behind you, they can swap with you for free.
10 * 
11 * <p data-end="755" data-start="634">Return an array answer of size n, where answer[i] is the <strong data-end="680" data-start="664">minimum total cost to reach each position i in the line<font face="monospace">.</font>
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">cost = [5,3,4,1,3,2]</span>
16 * Output: <span class="example-io">[5,3,3,1,1,1]</span>
17 * Explanation:
18 * We can get to each position in the following way:
19 * 
20 * 	i = 0. We can swap with person 0 for a cost of 5.
21 * 	<span class="example-io"><font face="monospace">i = </font>1. We can swap with person 1 for a cost of 3.</span>
22 * 	<span class="example-io">i = 2. We can swap with person 1 for a cost of 3, then swap with person 2 for free.</span>
23 * 	<span class="example-io">i = 3. We can swap with person 3 for a cost of 1.</span>
24 * 	<span class="example-io">i = 4. We can swap with person 3 for a cost of 1, then swap with person 4 for free.</span>
25 * 	<span class="example-io">i = 5. We can swap with person 3 for a cost of 1, then swap with person 5 for free.</span>
26 * </div>
27 * <strong class="example">Example 2:
28 * <div class="example-block">
29 * Input: <span class="example-io">cost = [1,2,4,6,7]</span>
30 * Output: <span class="example-io">[1,1,1,1,1]</span>
31 * Explanation:
32 * We can swap with person 0 for a cost of <span class="example-io">1, then we will be able to reach any position i for free.</span>
33 * </div>
34 *  
35 * Constraints:
36 * 
37 * 	1 <= n == cost.length <= 100
38 * 	1 <= cost[i] <= 100
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/minimum-cost-to-reach-every-position/
44// discuss: https://leetcode.com/problems/minimum-cost-to-reach-every-position/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn min_costs(cost: Vec<i32>) -> Vec<i32> {
50        vec![]
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_3502() {
62    }
63}
64

Back
© 2026 bowen.ge All Rights Reserved.