原题

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

解析

分糖

给一个数组,这个数组有偶数个,要均分给弟弟和妹妹两人

数组的每一个元素代表一颗糖,元素的一种值代表一种糖

求分给妹妹最多多少种糖

思路

要给妹妹分最多的糖,但妹妹只能拿一半个数的糖,所以如果糖的种数大于半数,则妹妹最多拿半数种类的糖

否则拿所有种类的糖,不够半数的其他糖种类也不会增加

我的解法

我的解法已经比较简单了,但是存在重复计算,忽略了Math.min这个方法

  1. public static int distributeCandies(int[] candies) {
  2. return Arrays.stream(candies).distinct().count() > candies.length / 2 ? candies.length / 2 : (int)
  3. Arrays.stream(candies).distinct().count();
  4. }

最优解

在leetcode上看到一种解法,也是一行,也用了java8新特性,但是他求糖的种类实现比较麻烦,如下

  1. public int distributeCandies(int[] candies) {
  2. return Math.min(candies.length / 2, IntStream.of(candies).boxed().collect(Collectors.toSet()).size());
  3. }

所以最优解可以结合一下(其实后面有人提交了一样的解法)

  1. public static int distributeCandiesOptimize(int[] candies) {
  2. return Math.min(candies.length / 2, (int) IntStream.of(candies).distinct().count());
  3. }

【leetcode】575. Distribute Candies的更多相关文章

  1. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

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

  2. 【leetcode】1103. Distribute Candies to People

    题目如下: We distribute some number of candies, to a row of n = num_people people in the following way: ...

  3. 【Leetcode_easy】1103. Distribute Candies to People

    problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...

  4. 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  5. 【leetcode】979. Distribute Coins in Binary Tree

    题目如下: Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and th ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. Mac下载工具软件提示损坏

    今天装Navicat的时候一直报错文件损坏,最后请教别人才知道,这里记录下: 原因: Mac默认不允许任何来源的软件安装,安全问题,需要我们设置下即可: 解决方法: 方法一: 方法二: 终端输入命令: ...

  2. Swift学习 (一)

    以后会自己总结学习Swift的笔记与深化.希望能够帮助已经有Objective-C经验的开发者更快地学习Swift.我们一起学习,同时也品味到Swift的精妙之处. 结论放在开头:我认为Swift比O ...

  3. [CareerCup] Single Valid Tree

    https://www.careercup.com/question?id=5103530547347456 Given a list of nodes, each with a left child ...

  4. MG7780打印机喷嘴堵塞

    1.深度清洗,打印喷嘴图案,发现有颜色没有打印出来 2.墨盒一加墨水就往外冒,以为是满的,其实是内部已经堵塞而加不进去,因为出墨口并没有墨水向外流(出墨口没有盖起来).解决办法就是用没有针头的针管从加 ...

  5. 连载三:RobotFramework+Selenium+Jenkins分布式构建

    目标:Jenkins安装在服务器上,而使用Jenkins调用本机的脚本并在本机执行. 步骤: (1)需要有RobotFrameWork+Selenium的运行环境: python2.7,Robotfr ...

  6. 利用 AWS 无服务架构之语音合成

    目录 一.架构图 二.服务部署 2.1.创建 DynamoDB 表 2.2.创建 S3 2.2.1 静态网页存储桶 2.2.2.音频存储桶 2.3.创建 SNS Topic 2.4.为 Lambda ...

  7. charles 开始/暂停记录

    本文参考:charles 开始/暂停记录 1.1. stop/start recording 和 2.1 recording settings 是常用的功能了:这里需要注意就是后面的session1代 ...

  8. sql语句中,取得schema中的所有表信息及表的定义结构

    postgressql下'検索スキーマの中で.全てテーブルselect tablename from pg_tables where schemaname='test' mysql下'検索スキーマの中 ...

  9. linux环境上报异常java.lang.NoSuchMethodError

    23-Apr-2019 18:11:35.545 INFO [http-nio-10052-exec-10] org.apache.catalina.core.ApplicationContext.l ...

  10. 《Mysql - 我的Mysql为什么会抖一下?》

    一: 抖一下? - 平时的工作中,不知道有没有遇到过这样的场景. - 一条 SQL 语句,正常执行的时候特别快,但是有时也不知道怎么回事,它就会变得特别慢. - 并且这样的场景很难复现,它不只随机,而 ...