【leetcode】575. 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].
解析
分糖
给一个数组,这个数组有偶数个,要均分给弟弟和妹妹两人
数组的每一个元素代表一颗糖,元素的一种值代表一种糖
求分给妹妹最多多少种糖
思路
要给妹妹分最多的糖,但妹妹只能拿一半个数的糖,所以如果糖的种数大于半数,则妹妹最多拿半数种类的糖
否则拿所有种类的糖,不够半数的其他糖种类也不会增加
我的解法
我的解法已经比较简单了,但是存在重复计算,忽略了Math.min这个方法
public static int distributeCandies(int[] candies) {
return Arrays.stream(candies).distinct().count() > candies.length / 2 ? candies.length / 2 : (int)
Arrays.stream(candies).distinct().count();
}
最优解
在leetcode上看到一种解法,也是一行,也用了java8新特性,但是他求糖的种类实现比较麻烦,如下
public int distributeCandies(int[] candies) {
return Math.min(candies.length / 2, IntStream.of(candies).boxed().collect(Collectors.toSet()).size());
}
所以最优解可以结合一下(其实后面有人提交了一样的解法)
public static int distributeCandiesOptimize(int[] candies) {
return Math.min(candies.length / 2, (int) IntStream.of(candies).distinct().count());
}
【leetcode】575. Distribute Candies的更多相关文章
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【leetcode】1103. Distribute Candies to People
题目如下: We distribute some number of candies, to a row of n = num_people people in the following way: ...
- 【Leetcode_easy】1103. Distribute Candies to People
problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...
- 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- Jenkins增加日志查看内容. 如何查看Jenkins插件的日志?
进入Jenkins日志项 添加新的日志记录 把插件的GroupID信息填入 对应的源代码 日志生成代码 执行插件,即可查看插件里的日志输出
- web-自动化测试流程
1. 自动化测试流程 (1) 需求分析 (2) 挑选适合做自动化测试的功能 (3) 设计测试用例 (4) 搭建自动化测试环境 [可选] (5) 设计自动化测试项目的架构 [可选] (6) 编写代码 ( ...
- Windows命令行更改有线本地连接IP地址及DNS的bat脚本
有些场景需要测试网络通不通等,每次打开网络和共享中心真烦,win10右下角还进不去了,shit! 运行ncpa.cpl也打开那个界面也是费劲吧啦,索性写个bat吧!为啥要写?还不是因为懒!可以把脚本复 ...
- nginx目录学习
目录 一. Nginx 基础知识 二. Nginx 安装及调试 三. Nginx Rewrite 四. Nginx Redirect 五. Nginx 目录自动加斜线: 六. Nginx Locati ...
- python函数声明和调用(18)
函数是指代码片段,可以重复调用,比如我们前面文章接触到的type()/len()等等都是函数,这些函数是python的内置函数,python底层封装后用于实现某些功能. 一.函数的定义 在Python ...
- ElasticSearch总结
转载自:https://www.cnblogs.com/WessonStar/p/8296781.html 不过我的es版本是7.2.0,然后这里要注意ik分词器放入plugins里是需要有个ik插件 ...
- [DEBUG] ubuntu pip安装成功却无法import
我的pip经常出问题,我也不知道为啥..今天搞啥啥坏=.= 问题: pip自动安装显示成功,在交互环境下却无法import ==========================踩坑========== ...
- 【C++札记】构造函数与析构函数
构造函数(constructor) 1.构造函数是种特殊的类成员函数,遵循如下规则: a.函数名与类名必须相同. b.没有返回值 例如: class Obj { ... public: Obj() { ...
- Python split()方法分割字符串
Python 中,除了可以使用一些内建函数获取字符串的相关信息外(例如 len() 函数获取字符串长度),字符串类型本身也拥有一些方法供我们使用. 注意,这里所说的方法,指的是字符串类型 str 本身 ...
- bs4解析
介绍:将一个html文档转换成BeautifulSoup对象,然后通过对象的方法或属性查找指定的节点内容 转换本地文件: soup = BeautifulSoup(fp,'lxml') fp为文档 ...