Cheapest Way to Gather Chips at One Position. Each chip starts at its given integer position. Moving one chip by+2 or-2 costs0; moving it by+1 or-1 costs1.
### Problem
Each chip starts at its given integer position. Moving one chip by +2 or -2 costs 0; moving it by +1 or -1 costs 1. Return the minimum total cost to bring all chips to the same position.
```cpp
class Solution {
public:
int minCostToMoveChips(vector<int>& position);
};
```
### Examples
**Example 1:**
```text
Input: position = [2,4,7]
Output: 1
Explanation: Move4 to2 for free and7 through odd positions to3 for free, then3 to2 for cost1.
```
**Example 2:**
```text
Input: position = [1,3,5]
Output: 0
Explanation: All positions are odd and can reach1 using free steps of size2.
```
### Constraints
- There are 1 to 100 chips.
- Each original position is an integer in `[1,1000000000]`.
### Contract considerations
The destination is a common integer position and need not be chosen from the original positions.
```hint Read the boundary condition
Can separate chips share one position, and is the objective a move count or the total paid cost?
```
### Boundary reminders
The destination is a common integer position and need not be chosen from the original positions.
Overview: Cheapest Way to Gather Chips at One Position. Each chip starts at its given integer position. Moving one chip by+2 or-2 costs0; moving it by+1 or-1 costs1.
Each chip starts at its given integer position. Moving one chip by +2 or -2 costs 0; moving it by +1 or -1 costs 1. Return the minimum total cost to bring all chips to the same position.
class Solution {
public:
int minCostToMoveChips(vector<int>& position);
};
Examples
Example 1:
Input: position = [2,4,7]
Output: 1
Explanation: Move4 to2 for free and7 through odd positions to3 for free, then3 to2 for cost1.
Example 2:
Input: position = [1,3,5]
Output: 0
Explanation: All positions are odd and can reach1 using free steps of size2.
Constraints
There are 1 to 100 chips.
Each original position is an integer in
[1,1000000000]
.
Contract considerations
The destination is a common integer position and need not be chosen from the original positions.
Boundary reminders
The destination is a common integer position and need not be chosen from the original positions.