【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 ...
随机推荐
- 意想不到的JavaScript(每日一题1)
问题: 答案: 解析:
- 重装java后hadoop配置文件的修改
1.删除hdfs-site.xml中dfs.namenode.name.dir目录和dfs.datanode.data.dir目录 然后 hdfs namenode -format 不然将无法启动na ...
- Git 的这个神技,学会爽歪歪~
现在大多数公司都有 GIT 来管理代码版本控制了,既然用到 GIT,相信大家都接触过 Github.Gitlab.Gitee 这些远程仓库,或者是公司内部自行搭建的 GIT 仓库. 当用到 SSH 方 ...
- CF 631B 题解
题面 注意到每次只染色一行或者一列,那么我们最后输出第i行第j列的数字是多少的时候只需要看一下最后一次i行和第j行被染了什么颜色,所以我们需要对每一行和一列记录最后一次染色的颜色. 但是我们也需要比较 ...
- Python 把较长的一行代码分成多行的技巧
概述:在写代码过程中,经常遇到一行代码很长的情况.为了让代码显得整齐干净,就需要把一行代码分成多行来写,Python中有三种小技巧可以实现该功能: 1.用反斜杠\链接多行代码 示例: ...
- mysql之general log 日志
开启 general log 将所有到达MySQL Server的SQL语句记录下来. 一般不会开启开功能,因为log的量会非常庞大.但个别情况下可能会临时的开一会儿general log以供排障使用 ...
- 模板 - 强连通分量/割点/桥 - Tarjan
int dfn[N], low[N], dfncnt, s[N], tp; int scc[N], sc; // 结点 i 所在 scc 的编号 int sz[N]; // 强连通 i 的大小 voi ...
- sql server 函数详解(1)字符串函数
ASCII()函数 CHAR()函数 LEFT()函数 RIGHT()函数 LTRIM()函数 RTRIM()函数 STR()函数 字符串逆序的函数REVERSE() 计算字符串的长度函数LEN(st ...
- h5与app混合开发,jsbridge
https://juejin.im/post/5bda6f276fb9a0226d18931f https://juejin.im/post/5abca877f265da238155b6bc http ...
- 机器学习-回归中的相关度和R平方值
1. 皮尔逊相关系数(Pearson Correlation Coefficient) 1.1 衡量两个值线性相关强度的量 1.2 取值范围[-1, 1] 正相关:>0, 负相关:<0, ...