【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 ...
随机推荐
- pcap中不同包功能
1.不同包协议的功能 EAPoL:基于局域网的扩展认证协议 ICMPv6:(一般是四个连在一起)互联网控制协议第六套 DHCP Discover:请求分配IP DHCP Offer:你的IP是***, ...
- LeetCode.976-周长最大的三角形(Largest Perimeter Triangle)
这是悦乐书的第368次更新,第396篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第230题(顺位题号是976).给定正长度的数组A,返回具有非零区域的三角形的最大周长, ...
- FPGA VGA时序的理解
最近在做FPGA毕业设计,毕业设计规划的是摄像头采集图像,经过均值滤波,中值滤波,高斯滤波,然后通过VGA接口控制显示器显示出来,所以最近学习了一下FPGA的VGA驱动的相关内容. VGA接口 如上图 ...
- word2vec高效训练方法
在word2vec原理中讲到如果每个词向量由300个元素组成,并且一个单词表中包含了10000个单词.回想神经网络中有两个权重矩阵——一个在隐藏层,一个在输出层.这两层都具有300 x 10000 = ...
- tensorflow keras导包混用
tensoboard 导入:导入包注意 否者会报错 :keras FailedPreconditionError: Attempting to use uninitialized value trai ...
- MySQL各种函数/语法
@limit pos,len select * from table limit 5,10 只显示查出结果的6-15行 ---------------------------------------- ...
- shell脚本一键部署nginx
一键部署nginx 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...
- [LeetCode] 223.矩形面积
题目链接: https://leetcode-cn.com/problems/rectangle-area 难度:中等 通过率:41.3% 题目描述: 在 二维 平面上计算出两个 由直线构成的 矩形重 ...
- <form:select>
<form:select path="classification" class="input-medium"> <form:option v ...
- The library 'libhostpolicy.dylib' required to execute the application was not found in
.NET Core应用程序需要runtimeconfig.json文件.此JSON文件配置运行时的选项.没有runtimeconfig.json文件,这将失败. > dotnet Program ...