博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
575. Distribute Candies
阅读量:6714 次
发布时间:2019-06-25

本文共 1489 字,大约阅读时间需要 4 分钟。

题目描述:

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: 3Explanation: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: 2Explanation: 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:

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

解题思路:

尽量将数目不同的糖果给姐姐。统计数目不同的糖果的数目n,当n大于candies.size()/2时返回candies.size()/2,否则返回candies.size()/2。

代码:

1 class Solution { 2 public: 3     int distributeCandies(vector
& candies) { 4 unordered_set
cand; 5 for (auto tmp : candies) 6 cand.insert(tmp); 7 if (cand.size() > candies.size()/2) 8 return candies.size() / 2; 9 else 10 return cand.size();11 }12 };

 

转载于:https://www.cnblogs.com/gsz-/p/9495000.html

你可能感兴趣的文章
I/O编程软件题(Java语言)
查看>>
时序逻辑、组合逻辑,我不再怕你了
查看>>
(三)mybatis之对Hibernate初了解
查看>>
git 分支( branch ) 的基本使用
查看>>
HDU 4334 Trouble
查看>>
nginx安装与配置
查看>>
Android 命令设置获取、IP地址、网关、dns
查看>>
弹性碰撞 poj 3684
查看>>
查找当前薪水(to_date='9999-01-01')排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不准使用order by...
查看>>
[SQL in Azure] Windows Azure Virtual Machine Readiness and Capacity Assessment
查看>>
关于CCR测评器的自定义校验器(Special Judge)
查看>>
java设计模式之 装饰器模式
查看>>
loadrunner-3-18Service-Level Agreement(服务水平协议)
查看>>
Python编程-基础知识-列表和元组
查看>>
利息力(force of interest)
查看>>
Oracle 角色及其权限
查看>>
NiftyDialogEffects:集成了多种动画效果的Dialog控件
查看>>
《世界是数字的》读后感
查看>>
AD软件原理图封装过程(即由原理图转换到PCB)
查看>>
cocos2d-x lua table与json的转换
查看>>