We have n chips, where the position of the ith chip is position[i].

We need to move all the chips to the same position. In one step, we can change the position of the ith chip from position[i] to:

  • position[i] + 2 or position[i] - 2 with cost = 0.
  • position[i] + 1 or position[i] - 1 with cost = 1.

Return the minimum cost needed to move all the chips to the same position.

Example 1:

Input: position = [2,2,2,3,3]
Output: 2
Explanation: We can move the two chips at position 3 to position 2. Each move has cost = 1. The total cost = 2.

Example 2:

Input: position = [1,1000000000]
Output: 1

这道题乍一看还挺复杂,但仔细想一想还是挺简单,题目的意思是将n个chips移动到一起最小的花销,而且移动的cost有规定,如果距离差一,则移动到一起cost=1,如果距离差二,则移动到一起cost=0,这句话隐含的含义是就是,如果两个chips之间距离为偶数的话,则cost=0,就可以移动到一起。如果距离是奇数的话,移动到一起cost=1。这样的话就可以将所有chips分成两组,奇数组和偶数组,然后将二者中chips个数小的往chips个数较多的移动即可,简而言之就是输出二者的最小值。

class Solution {
public:
int minCostToMoveChips(vector<int>& position) {
// 如果位置之间的差距是2的倍数 则可以化为一类
// 如果位置之间的差距是奇数则 则差距为1
// 所以可以将所有位置拆分为奇数 或者偶数
// 奇数position 移动到1 的cost 都为0 偶数position 移动到2 的cost 都为0
int dp[2]={0};
for(auto pp:position){
if(pp&1==1){
dp[1]+=1;
}
else{
dp[0]+=1;
}
}
return min(dp[0],dp[1]);
}
};

【leetcode】1217. Minimum Cost to Move Chips to The Same Position的更多相关文章

  1. 【LeetCode】1167. Minimum Cost to Connect Sticks 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetcod ...

  2. 【LeetCode】857. Minimum Cost to Hire K Workers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  3. 【leetcode】983. Minimum Cost For Tickets

    题目如下: In a country popular for train travel, you have planned some train travelling one year in adva ...

  4. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  5. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  6. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  7. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

    今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...

  8. 【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 tw ...

  9. 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

随机推荐

  1. Android WebView 实现文件选择、拍照、录制视频、录音

    原文地址:Android WebView 实现文件选择.拍照.录制视频.录音 | Stars-One的杂货小窝 Android中的WebView如果不进行相应的设置,H5页面的上传按钮是无法触发And ...

  2. scrapy 的response 的相关属性

    Scrapy中response介绍.属性以及内容提取   解析response parse()方法的参数 response 是start_urls里面的链接爬取后的结果.所以在parse()方法中,我 ...

  3. 寒武纪加速平台(MLU200系列) 摸鱼指南(一)--- 基本概念及相关介绍

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  4. istio ServiceMesh

    什么是ServiceMesh?什么是Istio? 微服务的一种概念,随着微服务的来临,衍生出一系列的问题,比如服务发现.负载均衡.路由.流量控制.服务间通讯的可靠性.微服务的监控等一系列的问题.使用a ...

  5. 解决虚拟机linux系统全屏问题

    修改设置 1) 如下图右单击虚拟机名,选择[settings-],调出虚拟机设置界面. 2) 在设置界面选择[hardware]->[CD/DVD2(IDE)]->[Connection] ...

  6. 一个校验接口引发的思考--我真的了解Response吗

    一个校验接口 最近,我需要对接一个外部接口,基本功能是:校验指定的门店是否完善了货运信息.接口大致是这样的: POST https://******/Dealer/CheckCarrier Heads ...

  7. 使用 @Transactional 时常犯的N种错误

    @Transactional是我们在用Spring时候几乎逃不掉的一个注解,该注解主要用来声明事务.它的实现原理是通过Spring AOP在注解修饰方法的前后织入事务管理的实现语句,所以开发者只需要通 ...

  8. myeclipse与tomcat,运行jsp程序

    一.myeclipse中配置JRE 步骤: 1.选择window->preferences->Java->Installed JREs 2.点击窗口右边的"add" ...

  9. 重新整理 .net core 实践篇——— 权限源码阅读四十五]

    前言 简单介绍一下权限源码阅读一下. 正文 一直有人对授权这个事情上争论不休,有的人认为在输入账户密码给后台这个时候进行了授权,因为认为发送了一个身份令牌,令牌里面可能有些用户角色信息,认为这就是授权 ...

  10. [hdu6978]New Equipments II

    显然可以费用流来做,具体建图如下-- 点集:源点,汇点,左边$n$​个工人,右边$n$​​​个设备 边集:源点向第$i$​个工人连$(1,a_{i})$​的边,第$i$​个设备向汇点连$(1,b_{i ...