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. 关于AI

    自己看着办吧 http://tieba.baidu.com/p/6008409988?fr=ala0&pstaala=1&tpl=5&fid=93764&isgod=0

  2. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  3. ansible迭代/迭代嵌套/同步异步/特殊topic说明

    tasks直接举例说明: ---- host: docker  remote_user: root  gather_facts: yes  serial: 3 #表示同一时间控制主机数量(值可以是数值 ...

  4. linux环境下安装lnmp出现php安装失败

    找到lnmp1.5/include/version.sh文件打开修改 #Freetype_Ver='freetype-2.7' 为 #Freetype_Ver='freetype-2.9'即可

  5. DirectX11--深入理解与使用2D纹理资源

    前言 写教程到现在,我发现有关纹理资源的一些解说和应用都写的太过分散,导致连我自己找起来都不方便.现在决定把这部分的内容整合起来,尽可能做到一篇搞定所有2D纹理相关的内容,其中包括: DDSTextu ...

  6. 在思科路由器上配置AAA实验(Cisco PT)

     1.拓扑图 Addressing Table 地址表    Device   Interface   IP Address   Subnet Mask   R1 Fa0/0 192.168.1.1 ...

  7. 学习itop4412开发板有哪些资料可学习?能否学会

    1.光盘资料 下面简单的做一下了解,在需要使用这些资料的时候,会针对性的做详细介绍. 将文件以及文件夹按照“名称+递增”的方式排列,如下: 01_PCB_SCH_DATASHEET-- -----开发 ...

  8. python模块------pyinotify

    介绍 pyinotify模块用来监测文件系统的变化,依赖于Linux内核的inotify功能,inotify是一个事件驱动的通知器,其通知接口从内核空间到用户空间通过三个系统调用.pyinotify结 ...

  9. 防盗链之URL参数签名

    一.概述 传统的 IP 禁用.referer 防盗链.User-Agent 防盗链.地区访问控制等防盗链措施已经无法完全满足用户要求,所以开发出URL参数签名方式来防盗链 二.实现 Token防盗链是 ...

  10. Linux apache的运行用户和用户组

    我们在安装apache后,有时在上传文件的时候,提示没有权限或者是不可写,我们都会去查文件夹的权限. 通过ls -l /var/www/html/website可以很直观的看出我们文件和文件夹的权限, ...