【leetcode】1217. Play with Chips
题目如下:
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).
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.Constraints:
1 <= chips.length <= 100
1 <= chips[i] <= 10^9
解题思路:最终所有的chips处于的位置要么是偶数位要是是奇数位:如果是偶数位,那么初始就处于偶数位chips移动到最终位置不需要任何cost,而初始处于奇数位的chips移动到最近的偶数位只需要1个cost;反之如果最终位置是奇数位也是一样的。所以答案就是初始处于奇数位的chips和除数处于偶数位的chips的数量的较小值。
代码如下:
class Solution(object):
def minCostToMoveChips(self, chips):
"""
:type chips: List[int]
:rtype: int
"""
odd = 0
even = 0
for i in chips:
if i % 2 == 1:
odd += 1
else: even += 1
return min(odd,even)
【leetcode】1217. Play with Chips的更多相关文章
- 【leetcode】1217. Minimum Cost to Move Chips to The Same Position
We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- jenkins 配置 gitlab webhook 实现自动发布
测试环境需要git提交代码后,Jenkins自动部署,需要gitlab配置project webhook. 1,Jenkins版本2.89 gitlab 8.11 2,Jenkins需要安装插件:G ...
- 基于高斯分布的异常检测(Anomaly Detection)算法
记得在做电商运营初期,每每为我们频道的促销活动锁取得的“超高”销售额感动,但后来随着工作的深入,我越来越觉得这里面水很深.商家运营.品类运营不断的通过刷单来获取其所需,或是商品搜索排名,或是某种kpi ...
- excel常用公式--数据清洗类
trim:去除单元格两端的空格. concat/&:连接单元格内的内容. mid: 提取字符串中间的字符串. left: 提取字符串左边的字符串. right: 提取字符串右边的字符串. ...
- Office_Word使用技巧大全(超全)
目录 不收藏不行的 word 使用技巧大全 三招去掉页眉那条横线 批量转换全角字符为半角字符 快速打开最后编辑的文档 格式刷的使用 删除网上 下载 资料的换行符(象这种 "↓" ) ...
- C++中多态的概念和意义
1,函数重写回顾: 1,父类中被重写的函数依然会继承给子类: 2,子类中重写的函数将覆盖父类中的函数: 1,重写父类当中提供的函数是因为父类当中提供的这个函数版本不能满足我们的需求,因此我们要重写: ...
- 基于Caffe训练AlexNet模型
数据集 1.准备数据集 1)下载训练和验证图片 ImageNet官网地址:http://www.image-net.org/signup.php?next=download-images (需用邮箱注 ...
- notes-19-05-10
一 mysql查找一个表中字段相同的数据 2019-05-10 15:51:03 多字段 ) 二 Referer的作用?2019-05-17 10:03:48 (来自网络) 1.防盗链我在www ...
- mysql优化--explain关键字
MySQL性能优化---EXPLAIN 参见:https://blog.csdn.net/jiadajing267/article/details/81269067 参见:https://www.cn ...
- echarts 给legend图例加个标题式文字设置为普通文本不可点击
legend: [ { orient: "horizontal", // 'vertical' x: "68%", // 'center' | 'left' | ...
- Echarts-数据的视觉映射
来源:官网,自己整理 数据可视化是 数据 到 视觉元素 的映射过程(这个过程也可称为视觉编码,视觉元素也可称为视觉通道). ECharts 的每种图表本身就内置了这种映射过程,比如折线图把数据映射到『 ...