[LeetCode] #1217. Play with Chips
1 min readMar 17, 2020
Environment: Python 3.7
Key technique: operator %
Example 1:
Input: chips = [1,2,3]
Output: 1
Explanation: Second chip will be moved to positon 3 with cost 1. First chip will be moved to position 3 with cost 0. Total cost is 1.
Example 2:
Input: chips = [2,2,2,3,3]
Output: 2
Explanation: Both fourth and fifth chip will be moved to position two with cost 1. Total minimum cost will be 2.
Analysis:
The 1–3 (Odd to Odd) location cost 0 and 1–2 (Odd to even) location cost 1. The even to odd and even to even case is the same. We need to calculate what number of Odd and even location and get the minimum answer.
Solution:
class Solution:
def minCostToMoveChips(self, chips: List[int]) -> int:
O=0
E=0
for x in chips:
if x % 2 == 0:
E += 1
else:
O += 1
return min(E, O)
Submitted result:
Reference:
https://leetcode.com/problems/play-with-chips/discuss/479830/Easy-python-solution-with-explanation