问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3798 访问。

给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果。你需要把这些糖果平均分给一个弟弟和一个妹妹。返回妹妹可以获得的最大糖果的种类数。

输入: candies = [1,1,2,2,3,3]

输出: 3

解析: 一共有三种种类的糖果,每一种都有两个。

最优分配方案:妹妹获得[1,2,3],弟弟也获得[1,2,3]。这样使妹妹获得糖果的种类数最多。

输入: candies = [1,1,2,3]

输出: 2

解析: 妹妹获得糖果[2,3],弟弟获得糖果[1,1],妹妹有两种不同的糖果,弟弟只有一种。这样使得妹妹可以获得的糖果种类数最多。

注意:

数组的长度为[2, 10,000],并且确定为偶数。

数组中数字的大小在范围[-100,000, 100,000]内。


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.

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.

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].


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3798 访问。

public class Program {

    public static void Main(string[] args) {
var words = new int[] { 1, 1, 2, 2, 3, 3 }; var res = DistributeCandies(words);
Console.WriteLine(res); Console.ReadKey();
} public static int DistributeCandies(int[] candies) {
var res = new HashSet<int>();
for(var i = 0; i < candies.Length; i++) {
if(!res.Contains(candies[i])) {
res.Add(candies[i]);
}
}
if(res.Count < candies.Length / 2) {
return res.Count;
}
return candies.Length / 2;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3798 访问。

3

分析:

显而易见,以上算法的时间复杂度为: 

最新文章

  1. EasyPR--中文开源车牌识别系统 开发详解(1)
  2. UI进阶 跳转系统设置相关界面的方法
  3. [NOIP摸你赛]Hzwer的陨石(带权并查集)
  4. Android作业分组与选题
  5. IOS--手势控制的使用
  6. SPF 简介
  7. linux:磁碟与档案系统管理
  8. 琐碎-关于StringTokenizer工具
  9. POJ1149 PIGS
  10. javascript中的for……in循环
  11. sizeof,一个其貌不扬的家伙(转)
  12. Java字符串找出4个字节长度的字符
  13. oracle expdp导出远程数据到本地
  14. 再起航,我的学习笔记之JavaScript设计模式03
  15. lesson - 7 vim 详解
  16. Dataset:利用Python将已有mnist数据集通过移动像素上下左右的方法来扩大数据集为初始数据集的5倍—Jason niu
  17. go 基础(二)
  18. [Localization] R-CNN series for Localization and Detection
  19. Yii2 Restful api设计--App接口编程
  20. 解决webview调用 goBack() 返回上一页自动刷新闪白的情况

热门文章

  1. 同一台机器oralce11g和12c公用一个监听器监听多个端口
  2. Active Directory - Creating Public and Personnel Share Folders via Script
  3. FileNotFoundError: [WinError 2] 系统找不到指定的文件。 解决方案
  4. C# 判断和创建目录路径
  5. 搭建jmeter+influxdb+grafana压测实时监控平台(超详细,小白适用)
  6. Shell基本语法---处理海量数据的cut命令
  7. 基于Vue的UI框架element el-table表格的自定义排序
  8. 洛谷p1120小木棍(剪枝优化)
  9. JAVA基础(jdk安装和环境变量的配置)
  10. PHP date_time_set() 函数