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

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

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

  1. class Solution {
  2. public:
  3. int distributeCandies(vector<int>& candies) {
  4. if (candies.size() == ) return ;
  5. std::sort(candies.begin(), candies.end());
  6. int distinct = ;
  7.  
  8. for (int i = ; i < candies.size(); i++) {
  9. if (candies[i-] != candies[i]) {
  10. distinct++;
  11. }
  12. }
  13.  
  14. //how many candies for each one, since amount of candies is even and only split to 2 parts
  15. int n = candies.size() / ;
  16. return std::min(n, distinct);
  17. }
  18. };

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. DataReader转Dictionary数据类型之妙用

    datareader转dictionary有很多用处,可以输出表中部分字段转实体字段,以前需要全部字段输出或者再建一个实体模型才行,这样就可以减少数据库的输出量了,特别是某些接口的格式化输出很方便. ...

  2. PTA编译总结求最大值及其下标

    代码: #include<stdio.h>    int main(void)    {    int i,index=0,n;    int a[10];    scanf(" ...

  3. EditText以及登录UI实现

    EditText是可以输入的文本框 <?xml version="1.0" encoding="utf-8"?><RelativeLayout ...

  4. java day01记录

    详细记录见本地基础培训资料 一.数据类型 /* 数据类型:Java是一种强类型语言,针对每一种数据都给出了明确的数据类型. 数据类型分类: A:基本数据类型 B:引用数据类型(类,接口,数组) 基本数 ...

  5. python12--字符串的比较 函数的默认值的细节 三元表达式 函数对象 名称空间 作用域 列表与字典的推导式 四则运算 函数的嵌套

     复习   1.字符串的比较; 2.函数的参数; ******实参与形参的分类: 3.函数的嵌套调用:     # 字符串的比较#  -- 按照从左往右比较每一个字符,通过字符对应的ascii进行比较 ...

  6. ant 执行jmeter

    构建-invoke ant -properties jmeter.home=/home/userapp/apps/apache-jmeter-5.0report.title=kyh_register_ ...

  7. vs 开发 win32 程序,调出控制台窗口,方便调试

    设置方法 项目 -> 属性 -> 生成事件 ->后期生成事件 -> 命令行 中添加 editbin /SUBSYSTEM:CONSOLE $(OutDir)\$(Project ...

  8. [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单

    Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...

  9. 《11招玩转网络安全》之第四招:low级别的DVWA SQL注入

    以DVWA为例,进行手工注入,帮助读者了解注入原理和过程. 1.启动docker,并在终端中执行命令: docker ps -a docker start LocalDVWA docker ps 执行 ...

  10. <发条游戏设计>粗翻——序言、

    序言——————————————————————— 优雅 就像在英语里的很多单词一样,“优雅”有着一些不同意义的解释.一些习惯性的用法常常带有“美”的意思:例如“她穿着一条优雅的裙子”,代表着一种完全 ...