题目如下:

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的更多相关文章

  1. 【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 ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. java中变量的线程安全性

    静态变量:线程非安全.静态变量即类变量,位于方法区,为所有对象共享,共享一份内存,一旦静态变量被修改,其他对象均对修改可见,故线程非安全.实例变量:单例模式(只有一个对象实例存在)线程非安全,非单例线 ...

  2. 微信小程序--catchtap&bindtap

    转自:https://www.cnblogs.com/heron-yu/p/7244481.html 转自:http://blog.csdn.net/xiaochun365/article/detai ...

  3. linxu passwd 给linux用户设置密码 命令

    [root@localhost ~]# passwd # 修改 root 用户的密码 passwd 给linux用户设置密码 命令 passwd www 直接passwd是当前用户设置密码 非交互式修 ...

  4. 使用fiddler 抓包app 网络连接不上的原因

    https://blog.csdn.net/m0_37554415/article/details/80434477· 设置完记得关掉fiddler 重启 基于fiddler 4 windown 10 ...

  5. Spring JdbcTemplate 和 NamedParameterJdbcTemplate 使用

    1.简单介绍 DAO层 的一般使用常见的是MyBatis 和 Hibernate,但是Hibernate是重量级的,而且学习成本较高,Mybatis 需要编写大量配置文件及接口文件,对于简单的项目应用 ...

  6. 【转帖】如何看待 HTTP/3 ?

    如何看待 HTTP/3 ? https://mp.weixin.qq.com/s/fC10Cyj6xjjwOCnqxX-Dvg 车小胖的公众号 转帖学习一下. 原创: 车小胖谈网络 车小胖谈网络 20 ...

  7. java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter的解决方案

    白天在实验室的电脑上的项目搭起来,晚上回到宿舍发现跑不起来了,网上查到的大多不是想要的答案. 最终的解决方案是maven clean一下再重新package.

  8. Java基础——Modifier类

    转自:https://www.cnblogs.com/baiqiantao/p/7478523.html   反射 Reflect Modifier 修饰符工具类 在查看反射相关的Class.Fiel ...

  9. C#实现Web链接启动应用程序

    C#实现Web链接启动应用程序 最近需要配合Web端实现用户点击链接来启动应用程序并且需要能够传参数给应用程序. 那么就可以使用注册表来实现这个功能 编写注册表可以在软件安装程序中加入,也可以在软件启 ...

  10. mysql注入大全及防御

    0.明白存在的位置:get型 post型 cookie型 http头注入 1.先测试注入点,注册框.搜索框.地址栏啥的,判断是字符型,搜索型还是数字型 字符型 1' and '1'='1 成功, 1' ...