leetcode算法: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]. 这道题是说 给我们一个数组 [1,1,2,2,3,3]
每个数字代表一种类型的糖果,这个数组也就是有两颗1类型糖果 两个2类型糖果 两个3类型糖果 现在要男孩和女孩数量上等分这些糖果,问女孩最多能得到多少种类的糖果 思想就是:
num1 = 总数量/2 num2 = 糖果种类数
如果num1 更多,那女孩一定能每种糖果得到一个以上,所以num2是答案
如果num2更大,那么女孩就不可能每种糖果都能拿到一个,所以num1就是答案 总数量就是数组的长度,糖果种类数就是把数组去重,看有多少个数字,可以用集合来做,
我的python代码:
class Solution(object):
def distributeCandies(self, candies):
"""
:type candies: List[int]
:rtype: int
"""
kinds = len( set(candies) )
nums = len(candies)/2
if kinds< nums:
return kinds
else:
return nums if __name__ == '__main__':
s = Solution()
res = s.distributeCandies([1,1,1,1,2,2,2,3,3,3])
print(res)
leetcode算法:Distribute Candies的更多相关文章
- LeetCode 575. Distribute Candies (发糖果)
Given an integer array with even length, where different numbers in this array represent different k ...
- LeetCode 1103. Distribute Candies to People
1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...
- LeetCode 575 Distribute Candies 解题报告
题目要求 Given an integer array with even length, where different numbers in this array represent differ ...
- LeetCode: 575 Distribute Candies(easy)
题目: Given an integer array with even length, where different numbers in this array represent differe ...
- LeetCode 1103. Distribute Candies to People (分糖果 II)
题目标签:Math 题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完. for loop可以计数糖果的数量,直到糖果发完.但是还是要遍历array 给people 发糖,这里要用到 index ...
- [LeetCode] 1103. Distribute Candies to People 分糖果
题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5 ...
- LeetCode算法题-Distribute Candies(Java实现)
这是悦乐书的第266次更新,第279篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第133题(顺位题号是575).给定具有偶数长度的整数数组,其中该数组中的不同数字表示不 ...
- LeetCode.1103-向人们分发糖果(Distribute Candies to People)
这是小川的第393次更新,第425篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第256题(顺位题号是1103).我们通过以下方式向一排n = num_people个人分 ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- LeetCode算法题-Subdomain Visit Count(Java实现)
这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...
随机推荐
- FineReport破解心得
注:写文档目的主要用于技术学习,了解技术帆软公司技术大牛是怎么设计系统,请勿用于商用. 一. 工具准备 1. javassist:class代码修改工具 2. ...
- 💒 es6 + canvas 开源 盖楼小游戏 完整代码注释 从零教你做游戏(一)
盖楼游戏 一个基于 Canvas 的盖楼游戏 Demo 预览 在线预览地址 (Demo Link) 手机设备可以扫描下方二维码 github https://github.com/bmqb/tower ...
- MySQL的bigint类型
bigint支持的数字的大小范围为:19位,存电话号码.有符号范围:-9223372036854775808 到 9223372036854775807 int支持的数字范围为:10位,有符号范围:- ...
- Shiro【授权过滤器、与ehcache整合、验证码、记住我】
前言 本文主要讲解的知识点有以下: Shiro授权过滤器使用 Shiro缓存 与Ehcache整合 Shiro应用->实现验证码功能 记住我功能 一.授权过滤器测试 我们的授权过滤器使用的是pe ...
- canvas实现将文字变成颗粒
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 归并排序(非递归,Java实现)
归并排序(非递归):自底向上 public class MergeSort { /** * @param arr 待排序的数组 * @param left 本次归并的左边界 * @param mid ...
- C# Int 类型线程不安全
之前统计报表算法做了一个优化,一个查询二十分钟导致客户端超时,优化到只需要5秒钟.后来发现for循环里数据合并的时候耗时,就用并行做优化.但是发现并行后丢居然数据(当然是因为List线程不安全). 前 ...
- Java飞机大战源代码
刚学不久java,做了一个飞机大战的小小小小游戏,现在把这个思路总结以及代码分享出来.大佬别吐槽(emmmmmm .....开发环境:jdk1.7 开发工具:eclipese PlanelJPanel ...
- webpack-dev-server 设置反向代理解决跨域问题
一.设置代理的原因 现在对前端开发的要求越来越高,并且随着自动化以及模块化的 诞生,前后端开发模式越来越流行.后端只负责接口,前端负责数据展示.逻辑处理.但是前后端开发模式,有一个重要的问题,就是跨域 ...
- ava集合---HashSet的源码分析
一.HasnSet概述 Hashset实现set接口,由哈希表(实际上是一个HashMap实例)支持.它不保证set的迭代顺序.特别是它不保证该顺序恒久不变.此类允许使用Null元素 一.HasnSe ...