https://leetcode.com/problems/distribute-candies/description/

题目比较长,总结起来很简单:有个整型数组,长度是偶数,把它分成两份,要求有一份里面数字的多样性最大。

思路:一个很简单的思路是,数出有多少不一样的数字n,然后一份有多少个m,取两个的最小值就行。

class Solution {
public:
int distributeCandies(vector<int>& candies) {
if (candies.size() == ) return ;
std::sort(candies.begin(), candies.end());
int distinct = ; for (int i = ; i < candies.size(); i++) {
if (candies[i-] != candies[i]) {
distinct++;
}
} //how many candies for each one, since amount of candies is even and only split to 2 parts
int n = candies.size() / ;
return std::min(n, distinct);
}
};

575. Distribute Candies的更多相关文章

  1. LeetCode 575. Distribute Candies (发糖果)

    Given an integer array with even length, where different numbers in this array represent different k ...

  2. LeetCode 575 Distribute Candies 解题报告

    题目要求 Given an integer array with even length, where different numbers in this array represent differ ...

  3. [LeetCode&Python] Problem 575. Distribute Candies

    Given an integer array with even length, where different numbers in this array represent different k ...

  4. 575. Distribute Candies 平均分糖果,但要求种类最多

    [抄题]: Given an integer array with even length, where different numbers in this array represent diffe ...

  5. LeetCode: 575 Distribute Candies(easy)

    题目: Given an integer array with even length, where different numbers in this array represent differe ...

  6. 【leetcode】575. Distribute Candies

    原题 Given an integer array with even length, where different numbers in this array represent differen ...

  7. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  8. 【Leetcode_easy】1103. Distribute Candies to People

    problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...

  9. LeetCode 1103. Distribute Candies to People

    1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...

随机推荐

  1. 树莓派中QT实现串口通讯

    树莓派中QT实现串口通讯 开发平台为QT 此博客QT使用的为WiringPi驱动 我使用的串口调试助手为 cutecom 先简单说一些开发过程中需要注意的问题 Linux 下设备为 tty ,对应在 ...

  2. PMP是什么,PMP最难的是哪些内容?

    目前在国内很多人还不了解PMP是什么,甚至不知道PMP认证的内容是哪些,考来有什么用,今天我这边就普及一下. PMP认证是美国项目管理协会发起的一项针对项目管理专业人士资格认证.取得认证需要学习项目管 ...

  3. Security+ 认证考过经验分享 802分飘过

    PART 1/考前准备 1.针对与新人.学生建议看每一节直播课程,老师会结合自己的工作工作经验讲解课程,可以帮助学生理解知识. 2.备考期间建议官方指导手册至少看两遍以上,我在结合自己的做题库时发现有 ...

  4. LA4255/UVa1423 Guess 拓扑排序 并查集

    评分稍微有一点过分..不过这个题目确确实实很厉害,对思维训练也非常有帮助. 按照套路,我们把矩阵中的子段和化为前缀和相减的形式.题目就变成了给定一些前缀和之间的大小关系,让你构造一组可行的数据.这个东 ...

  5. P4177 [CEOI2008]order 网络流,最小割,最大权闭合子图

    题目链接 \(Click\) \(Here\) 如果没有租用机器就是一个裸的最大权闭合子图.现在有了租用机器应该怎么办呢? 单独拆点是不行的,因为会和直接买下的情况脱离关系,租借是和连边直接相关的,那 ...

  6. Ansible-基础

    Ansible架构 Inventory   主机清单,可以对主机分组 ansible-hoc   ansible的命令,适用临时场景 ansible-playbook   ansible是一个场景的集 ...

  7. 目前的.NET 主流的后端ORM框架

    转自:https://www.cnblogs.com/yeminglong/p/9518438.html  推荐一些常用的asp.net ORM框架 SqlSugar (国内) Dos.ORM (国内 ...

  8. 我的mybatis从oracle迁移转换mysql的差异【原】

    仅此作为笔记 分页差异 oracle <select id="select" parameterClass="java.util.Map" resultC ...

  9. 责任链模式-Chain of Responsibility(Java实现), 例2

    责任链模式-Chain of Responsibility 在这种模式中,通常每个接收者都包含对另一个接收者的引用.如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推. 咱们在 ...

  10. CSS定位 深入理解定位(position)的偏移

    前言 CSS有三种基本的布局机制:普通流.浮动和绝对定位.利用定位,可以准确地定义元素框相对于其正常位置应该出现的位置,或者相对于父元素.另一个元素甚至浏览器窗口本身的位置.但元素究竟如何定位,定位到 ...