博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode算法:Distribute Candies
阅读量:4515 次
发布时间:2019-06-08

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

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]. 这道题是说 给我们一个数组 [1,1,2,2,3,3] 每个数字代表一种类型的糖果,这个数组也就是有两颗1类型糖果 两个2类型糖果 两个3类型糖果 现在要男孩和女孩数量上等分这些糖果,问女孩最多能得到多少种类的糖果 思想就是: num1 = 总数量/2    num2 = 糖果种类数 如果num1 更多,那女孩一定能每种糖果得到一个以上,所以num2是答案 如果num2更大,那么女孩就不可能每种糖果都能拿到一个,所以num1就是答案 总数量就是数组的长度,糖果种类数就是把数组去重,看有多少个数字,可以用集合来做, 我的python代码:
1 class Solution(object): 2     def distributeCandies(self, candies): 3         """ 4         :type candies: List[int] 5         :rtype: int 6         """ 7         kinds = len( set(candies) ) 8         nums = len(candies)/2 9         if kinds< nums:10             return kinds11         else:12             return nums13 14 15 if __name__ == '__main__':16     s = Solution()17     res = s.distributeCandies([1,1,1,1,2,2,2,3,3,3])18     print(res)

 

转载于:https://www.cnblogs.com/Lin-Yi/p/7501745.html

你可能感兴趣的文章
STM32 C++编程 002 GPIO类
查看>>
无线冲方案 MCU vs SoC
查看>>
进程装载过程分析(execve系统调用分析)
查看>>
在windows 7中禁用media sense
查看>>
ELK-Elasticsearch安装
查看>>
Android 模拟器(Emulator)访问模拟器所在主机
查看>>
删除字符串中指定子串
查看>>
day40-socket编程
查看>>
SpringBoot里mybatis查询结果为null的列不返回问题的解决方案
查看>>
为什么留不住优秀的员工
查看>>
Django后台管理admin笔记
查看>>
JavaScript中的变量
查看>>
iptables基本原理和规则配置
查看>>
ArcGIS JS 学习笔记4 实现地图联动
查看>>
ubuntu 12.04 lts安装golang并设置vim语法高亮
查看>>
编程题目:PAT 1004. 成绩排名 (20)
查看>>
使用分层实现业务处理
查看>>
Microsoft Windows平台的NoSQL数据存储引擎
查看>>
浅谈虚拟机
查看>>
Ubuntu系统Linux编译osg库
查看>>