当前位置:网站首页>LeetCode 1217 玩筹码[贪心] HERODING的LeetCode之路
LeetCode 1217 玩筹码[贪心] HERODING的LeetCode之路
2022-07-21 14:40:00 【HERODING23】
解题思路:
本质上是统计偶数位上的筹码和奇数位上的筹码和,然后进行比较,因为偶数位和奇数位上的筹码各自转换是不需要cost的,那么就可以理解为最后所有奇数位筹码在一个奇数位上,所有偶数位筹码在一个偶数位上,然后少的放入大的上面,这就是最小的cost,代码如下:
class Solution {
public:
int minCostToMoveChips(vector<int>& position) {
int count1 = 0;
int count2 = 0;
for(int i = 0; i < position.size(); i ++) {
if(position[i] % 2 == 0) {
count1 += 1;
} else {
count2 += 1;
}
}
return min(count1, count2);
}
};
边栏推荐
- 西农大 C plus
- Length unit, color unit, text style and font style
- Vector container member function reserve() and iterator failure
- Rotation of 2D conversion, transform origin of 2D conversion center point and scale of 2D conversion
- 关于面试的问题2
- While and do while loops
- Set the background color, background range, Sprite chart, gradient, radial gradient, etc
- Sword finger offer II 100 Sum of minimum paths in triangle
- . Review the old and know the new: [6] what is LINQ
- Idea shortcut key (day 16)
猜你喜欢
聚醚/聚丙烯酰胺-竣甲基/聚丙烯酰胺/粒状聚N-异丙基丙烯酰胺壳聚糖水凝胶的制备方法
Seaborn的使用
Leetcode skimming: middle order traversal of binary trees
Using LINGO to solve simple linear programming problems
一键免费下载外文文献的方式
IDEA 搭建和环境变量
Leetcode brush question: circular linked list and circular linked list II
抽象类与接口
Idea setup and environment variables
详细教你用NPS搭建内网穿透服务,外出时轻薄本轻松连接家里的游戏主机远程玩耍
随机推荐
[MATLAB]:基础知识学习
网页的结构
浏览器跨域请求之credentials
动画,及动画的基本使用
单域名SSL证书和多域名SSL证书有什么区别?
Leetcode skimming: the penultimate node in the linked list
How to avoid the risk of mismatch between Ethernet interface and wiring
List labels and hyperlinks
异常处理丨一个小案例,带你解决NullPointerException
Leetcode skimming: reverse the linked list and the intermediate nodes in the linked list
面试遇到的问题
抽象类与接口
DOM event proxy (2)
Leetcode skimming: middle order traversal of binary trees
leetcode1588. Sum of all odd length subarrays
Episode 3 best B tutorial of VMware virtual machine installation (14 days)
Set the background color, background range, Sprite chart, gradient, radial gradient, etc
列表标签和超链接
西农大 C plus
Outdoor resource optical fiber management