There are some chips, and the i-th chip is at position chips[i]
.
You can perform any of the two following types of moves any number of times (possibly zero) on any chip:
- Move the
i
-th chip by 2 units to the left or to the right with a cost of 0. - Move the
i
-th chip by 1 unit to the left or to the right with a cost of 1.
There can be two or more chips at the same position initially.
Return the minimum cost needed to move all the chips to the same position (any position).
移动一格cost1,移动2格cost0,本质上偶数位置的可以免费移动到任意偶数位,奇数位可以免费移动到任意奇数位,那实际上就可以把奇的都放到一个位置上,偶的放到一个位置上,然后哪边少就把少的移动到多的去。
class Solution(object): def minCostToMoveChips(self, chips): """ :type chips: List[int] :rtype: int """ odd = 0 even = 0 for chip in chips: if chip % 2 == 0: even += 1 else: odd += 1 return min(even, odd)