575. Distribute Candies*

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

题目描述

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.

Example 1:

Input: candies = [1,1,2,2,3,3]
Output: 3
Explanation:
There are three different kinds of candies (1, 2 and 3), and two candies for each kind.
Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too.
The sister has three different kinds of candies.

Example 2:

Input: candies = [1,1,2,3]
Output: 2
Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1].
The sister has two different kinds of candies, the brother has only one kind of candies.

Note:

  • The length of the given array is in range [2, 10,000], and will be even.
  • The number in given array is in range [-100,000, 100,000].

C++ 实现 1

这道题解决重点在理清题意. 首先, 数组中的数字表示的是 candy 的类别, 而不是颗数. 为了解决这倒题, 需要先统计有多少个类别, 由于女孩只能拿一半个数的 candy, 为了能拿到更多的类别, 她可以每种类别挑一颗糖. 如果说一半个数, 假设为 count, 小于或等于总的类别数, 那么女孩最多拿到 count 个类别的糖; 否则, 女孩可以拿到所有类别的糖.

class Solution {public:int distributeCandies(vector<int>& candies) {int res = 0, count = candies.size() / 2;unordered_set<int> record(candies.begin(), candies.end());return count <= record.size() ? count : record.size();}
};

575. Distribute Candies*相关推荐

  1. []575. Distribute Candies

    problem 575. Distribute Candies 理解题意:主要是计算candies的种类数. 如果大于candies数目的一半,则返回一半的值,否则,返回candies的种类数. so ...

  2. LeetCode 575. Distribute Candies

    575. Distribute Candies 一.问题描述 Given an integer array with even length, where different numbers in t ...

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

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

  4. LeetCode | 575. Distribute Candies

    题外话:又是N久没有更博,今后争取每天总结. Leetcode上的新题,难度是Easy,我是按照acceptance刷的,所以经常遇到新题^_________^ 题目: Given an intege ...

  5. Distribute Candies

    Distribute Candies int distributeCandies(vector& candies) { int cnt = 1;//初始有一种int N = candies.s ...

  6. C#LeetCode刷题之#575-分糖果​​​​​​​(Distribute Candies)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3798 访问. 给定一个偶数长度的数组,其中不同的数字代表着不同种 ...

  7. leetcode刷题笔记(3)(python)

    485 Max Consecutive Ones 题意:给出一串二进制数组,求数组中最长的连续1的个数 思路:遍历数组判断,然后将值添加到长度保存数组中,取保存数组最大值.本题要考虑输入的数组为[0] ...

  8. LeetCode 简单算法题

    使用Nodejs 抓取的LeetCode 简单算法题  一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...

  9. LeetCode github集合,附CMU大神整理笔记

    Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步.github目前同步的题目是2020-09-17日之后写的题.之前写过的题会 ...

最新文章

  1. 「倚天」一出,谁与争锋!全球首款5nm服务器芯片,业界最强
  2. HiveSQL常用数据处理语句
  3. Java高阶语法---final
  4. 如何优化代码和RAM大小
  5. JAVA如何隐藏异常堆栈_java – 如何在控制台上停止打印异常堆栈跟踪?
  6. java kafka 分区_Java kafka如何实现自定义分区类和拦截器
  7. 修改Mac版Word默认字体的方法
  8. Tomact运行不起来,打开startup.bat 一闪而过
  9. 关于 Failed to resolve: org.jetbrains.kotlin:kotlin-stdlib-jre7的错误
  10. 哈理工c语言,哈理工C语言试题.doc
  11. 卷积神经网络以LeNet网络为例的手写体识别
  12. Grouping BP has not been assigned to any customer accounts groupMessage no. FSBP_ECC004
  13. deviceOrientation简介
  14. PHP语法基础篇——除法取整和取余数
  15. 2021-09-29 每天几个LCEDA小知识——如何修改元器件标签位置
  16. 关于程序员这个职业缺点总结
  17. 瑞星木马防御专杀QQ,360,百度软件管家(见图为证!) 1
  18. phonegap(cordova) 自定义插件代码篇(三)----支付宝支付工具整合
  19. Kafka中steamAPi操作
  20. 知到智慧树-2023译路畅通:英汉互译单元测试答案(全)

热门文章

  1. matlab力学实验,Matlab在力学课程课堂教学和虚拟实验中的应用
  2. 视觉测量时的工业相机和远心镜头选型方法
  3. 创新PC应用、打通云端体验,360小程序引发SaaS软件变革
  4. 电脑硬件及电脑配置知识大全
  5. laravel阿里SDK对接
  6. 进制转换与进制转换表达式
  7. 10月25日, win8 来了
  8. (一)Flume概述及快速入门
  9. 一个成功软件测试项目的经验(转载)
  10. 23.5 MySQL架构