【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 <= 1001 <= 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 ...
随机推荐
- 2018-12-10 发布 vue全家桶实现的商城web-app,真实数据接口开发
项目地址:https://github.com/Rosen97/web-shop.git 博客地址:https://segmentfault.com/a/1190000017323841
- 【VS开发】VS2013多字节工程问题uilding an MFC project for a non-Unicode character set is deprecated
VS2013多字节工程问题 使用VS2013编译旧版VC++程序时,提示Building an MFC project for a non-Unicode character set is depre ...
- elasticsearch-analysis-ik windows 环境 IK 中文分词器 的 下载 和 安装
1,下载插件压缩包(本地测试建议用迅雷下,生产用的绝对不要用迅雷下),链接地址:https://github.com/medcl/elasticsearch-analysis-ik/releases/ ...
- tableau分布式添加节点
参考: 两节点的安装:https://zhuanlan.zhihu.com/p/44732932https://help.tableau.com/current/server-linux/zh-cn/ ...
- [Python3] 028 常用模块 datetime
目录 datetime 1. datetime.date 2. datetime.time 3. datetime.datetime 4. datetime.timedelta 补充 datetime ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- Qt两个类通过信号槽通信
qt需要通过信号槽来通信,connect的时候总是返回false,请教了公司的一个小哥,才解决了问题,虽然是个很白痴的问题. bool b = QObject::connect(m_pCollectO ...
- Luogu P3953 [NOIP2017]逛公园
题目 首先我们跑出从\(1\)出发的最短路\(d1\)和反图上从\(n\)出发的最短路\(dn\). 然后我们处理出长度不超过\(d1_n+k\)的最短路边集,给它拓扑排序. 如果存在环,那么这个环一 ...
- 如何快速的查找服务所在的进程id?
执行 pgrep -l 服务名称 这里以查找redis所在的进程id为例 执行 pgrep -l redis 从图中可知进程id 为30058
- RocketMQ吐血总结
RocketMQ吐血总结 架构 概念模型 最基本的概念模型与扩展后段概念模型 存储模型 RocketMQ吐血总结 User Guide RocketMQ是一款分布式消息中间件,最初是由阿里巴巴消息中间 ...