作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/distribute-candies/#/description

题目描述

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:

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

题目大意

题目的意思是把一堆数字平均分成两堆,其中的一堆的最大的种类个数为多少。

解题方法

Java解法

第一感觉就是HashMap,统计每个数字出现的次数,然后统计就行,如果种类的数目多于总数字的半数,那么只能选半数的,否则不能平均分成两堆,否则就可以选择种类的个数做最多的分类个数。

public class Solution {
public int distributeCandies(int[] candies) {
Set<Integer> kinds = new HashSet<Integer>();
for(int candy : candies){
kinds.add(candy);
}
return Math.min(kinds.size(), candies.length / 2);
}
}

Python解法

如果蜡烛的种类够N//2种,那么可以给其中一堆N//2种,否则最多只能给蜡烛种类数个不同种类的蜡烛。

class Solution:
def distributeCandies(self, candies):
"""
:type candies: List[int]
:rtype: int
"""
return min(len(set(candies)), len(candies) // 2)

日期

2017 年 5 月 8 日
2018 年 11 月 8 日 —— 项目进展缓慢

【LeetCode】575. Distribute Candies 解题报告(Java & Python)的更多相关文章

  1. LeetCode 575 Distribute Candies 解题报告

    题目要求 Given an integer array with even length, where different numbers in this array represent differ ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  4. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  5. 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

  6. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  7. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  8. 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...

  9. LeetCode 575. Distribute Candies (发糖果)

    Given an integer array with even length, where different numbers in this array represent different k ...

随机推荐

  1. R shinydashboard——3.外观

    目录 1.皮肤 2.注销面板 3.CSS 4. 标题延长 5.侧边栏宽度 6.图标 7.状态和颜色 1.皮肤 shinydashboard有很多颜色主题和外观的设置.默认为蓝色,可指定黑丝.紫色.绿色 ...

  2. Identity Server 4 从入门到落地(四)—— 创建Web Api

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  3. 日常Java测试 2021/11/14

    课堂测试三 package word_show; import java.io.*;import java.util.*;import java.util.Map.Entry; public clas ...

  4. django中的filter(), all(), get()

    1. 类名.objects中的get(), filter(), all() 的区别 结论: (1)all()返回的是QuerySet对象,程序并没有真的在数据库中执行SQL语句查询数据,但支持迭代,使 ...

  5. 【vector+pair】洛谷 P4715 【深基16.例1】淘汰赛

    题目:P4715 [深基16.例1]淘汰赛 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 这道题因为数据范围不大,所以做法可以非常简单,使用一个vector加上pair就可以了: ...

  6. 3.6 String 与 切片&str的区别

    The rust String  is a growable, mutable, owned, UTF-8 encoded string type. &str ,切片,是按UTF-8编码对St ...

  7. mango后台

     环境搭建 项目配置 下载后导入项目,删除mvnw.mvnw.cmd两个文件 修改spring-boot-starter-web pom.xml --> run as --> mave i ...

  8. Dubbo消费者异步调用Future使用

    Dubbo的四大组件工作原理图,其中消费者调用提供者采用的是同步调用方式.消费者对于提供者的调用,也可以采用异步方式进行调用.异步调用一般应用于提供者提供的是耗时性IO服务 一.Future异步执行原 ...

  9. Dubbo使用Zookeeper注册中心

    在生产环境下使用最多的注册中心为Zookeeper,当然,Redis也可以做注册中心 一.创建提供者02-provider-zk (1) 导入依赖 https://blog.csdn.net/u012 ...

  10. java中子类继承父类什么?

    1.继承public和protected修饰的属性和方法,不管子类和父类是否在同一个包: 2.继承默认权限修饰符修饰的属性和方法,前提是子类和父类在同一个包.