1217. Minimum Cost to Move Chips to The Same Position Easy

@problem@discussion
#Array#Math#Greedy



1/**
2 * [1217] Minimum Cost to Move Chips to The Same Position
3 *
4 * We have n chips, where the position of the i^th chip is position[i].
5 * We need to move all the chips to the same position. In one step, we can change the position of the i^th chip from position[i] to:
6 * 
7 * 	position[i] + 2 or position[i] - 2 with cost = 0.
8 * 	position[i] + 1 or position[i] - 1 with cost = 1.
9 * 
10 * Return the minimum cost needed to move all the chips to the same position.
11 *  
12 * Example 1:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2020/08/15/chips_e1.jpg" style="width: 750px; height: 217px;" />
14 * Input: position = [1,2,3]
15 * Output: 1
16 * Explanation: First step: Move the chip at position 3 to position 1 with cost = 0.
17 * Second step: Move the chip at position 2 to position 1 with cost = 1.
18 * Total cost is 1.
19 * 
20 * Example 2:
21 * <img alt="" src="https://assets.leetcode.com/uploads/2020/08/15/chip_e2.jpg" style="width: 750px; height: 306px;" />
22 * Input: position = [2,2,2,3,3]
23 * Output: 2
24 * Explanation: We can move the two chips at position  3 to position 2. Each move has cost = 1. The total cost = 2.
25 * 
26 * Example 3:
27 * 
28 * Input: position = [1,1000000000]
29 * Output: 1
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	1 <= position.length <= 100
35 * 	1 <= position[i] <= 10^9
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/
41// discuss: https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn min_cost_to_move_chips(position: Vec<i32>) -> i32 {
47        0
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_1217() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.