1217.玩筹码
链接:1217.玩筹码
难度:Easy
标签:贪心、数组、数学
简介:返回将所有筹码移动到同一位置上所需要的 最小代价 。
题解 1 - cpp
- 编辑时间:2022-07-08
 - 内存消耗:7MB
 - 编程语言:cpp
 - 解法介绍:因为跳一格 1 消费,跳两格 0 消费,相当于只有在相邻格才会消费。
 
class Solution {
   public:
    int minCostToMoveChips(vector<int>& position) {
        int ans1 = 0, ans2 = 0;
        for (auto& num : position) {
            if (num & 1)
                ans1++;
            else
                ans2++;
        }
        return min(ans1, ans2);
    }
};