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. 并发编程从零开始(十四)-Executors工具类

    并发编程从零开始(十四)-Executors工具类 12 Executors工具类 concurrent包提供了Executors工具类,利用它可以创建各种不同类型的线程池 12.1 四种对比 单线程 ...

  2. C++ 指针的引用和指向引用的指针

    指向引用的指针 简单使用指针的一个例子就是: int a = 1; int *p = &a; 预先强调: 没有指向引用的指针 原因: 因为引用 不是对象,没有地址. 但是指向引用的指针是什么形 ...

  3. 重磅|Apache ShardingSphere 5.0.0 即将正式发布

    Apache ShardingSphere 5.0.0 GA 版在经历 5.0.0-alpha 及 5.0.0-beta 接近两年时间的研发和打磨,终于将在 11 月份与大家正式见面! 11 月 10 ...

  4. S2-002漏洞分析

    漏洞概述 Struts2-002是一个 XSS 漏洞,该漏洞发生在 <s:url> 和 <s:a>标签中,未对标签内字符进行转义,当标签的属性 includeParams=al ...

  5. 日志框架-logtube

    Logtube 是什么 logtube 框架是基于 slf4j的一个日志框架封装, 源码地址: https://github.com/logtube 基于 SLF4J框架, 扩展了日志输出格式 (兼容 ...

  6. Linux下搭建FFmpeg开发调试环境

    背景 如果你是一个FFmpeg的使用者,那么绝大部分情况下只需要在你的程序中引用FFmpeg的libav*相关的头文件,然后在编译阶段链接相关的库即可.但是如果你想调试FFmpeg内部相关的逻辑,或者 ...

  7. 业务领先模型(Business Leadership Model; BLM)

    1.什么是业务领先模型 业务领先模型是指是一个完整的战略规划方法论.这套方法论是IBM在2003年的时候,和美国某商学院一起研发的.后来,这个方法论成为IBM公司全球从公司层面到各个业务部门共同使用的 ...

  8. python实现膨胀与腐蚀

    目录: (一)膨胀 (二)腐蚀 (三)腐蚀代码(erode) (四)膨胀代码(dilate) (一)膨胀(或) (二)腐蚀(与) (三)腐蚀代码(erode) 1 def erode_demo(ima ...

  9. [loj3366]嘉年华奖券

    联系绝对值的几何意义/分类讨论,不难发现若$n$张奖券上的数从小到大依次为$a_{i}$,则收益为$\sum_{i=1}^{\frac{n}{2}}a_{i+\frac{n}{2}}-a_{i}$ 假 ...

  10. 手写HashMap,快手面试官直呼内行!

    手写HashMap?这么狠,面试都卷到这种程度了? 第一次见到这个面试题,是在某个不方便透露姓名的Offer收割机大佬的文章: 这--我当时就麻了,我们都知道HashMap的数据结构是数组+链表+红黑 ...