Class Solution
java.lang.Object
g1201_1300.s1217_minimum_cost_to_move_chips_to_the_same_position.Solution
1217 - Minimum Cost to Move Chips to The Same Position.<p>Easy</p>
<p>We have <code>n</code> chips, where the position of the <code>i<sup>th</sup></code> chip is <code>position[i]</code>.</p>
<p>We need to move all the chips to <strong>the same position</strong>. In one step, we can change the position of the <code>i<sup>th</sup></code> chip from <code>position[i]</code> to:</p>
<ul>
<li><code>position[i] + 2</code> or <code>position[i] - 2</code> with <code>cost = 0</code>.</li>
<li><code>position[i] + 1</code> or <code>position[i] - 1</code> with <code>cost = 1</code>.</li>
</ul>
<p>Return <em>the minimum cost</em> needed to move all the chips to the same position.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2020/08/15/chips_e1.jpg" alt="" /></p>
<p><strong>Input:</strong> position = [1,2,3]</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> First step: Move the chip at position 3 to position 1 with cost = 0. Second step: Move the chip at position 2 to position 1 with cost = 1. Total cost is 1.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2020/08/15/chip_e2.jpg" alt="" /></p>
<p><strong>Input:</strong> position = [2,2,2,3,3]</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong> We can move the two chips at position 3 to position 2. Each move has cost = 1. The total cost = 2.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> position = [1,1000000000]</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= position.length <= 100</code></li>
<li><code>1 <= position[i] <= 10^9</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minCostToMoveChips
public int minCostToMoveChips(int[] position)
-