解题思路
单数移动到单数 代价始终是0
双数移动到双数 代价始终是0
题目中数组中的123.代表位置。比如:[1,2,2,2,3,3] 代表 第一个位置有1个筹码,第二个位置有三个筹码,第三个位置有2个筹码。
解题思路:
将问题转化为,是单数位置上的筹码还是双数位置上的筹码少。
代码
class Solution {
public int minCostToMoveChips(int[] chips) {
int single=0;
int double_=0;
for(int chip:chips) {
if(chip%2==0) {
double_++;
}
else {
single++;
}
}
if(double_<single){
return double_;
}else{
return single;
}
}
}