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].
分析:数字编号的不同代表糖果种类的不同,将一堆糖果平均分给俩兄妹,妹妹的种类要最多。
思路:只存在两种情况:种类数大于等于总数的一半时,妹妹会有一半种类数的糖果;种类数小于总数的一半时,妹妹会拿到所有种类数的糖果。
JAVA CODE
class Solution {
public int distributeCandies(int[] candies) {
// 需要将整型数组转成interger,因为基本类型不能直接转成set。
Integer[] cc = new Integer[candies.length];
for (int i = 0; i < cc.length; i++)
cc[i] = new Integer(candies[i]);
return Math.min(new HashSet<Integer>(Arrays.asList(cc)).size(), candies.length/2);
}
}
Distribute Candies的更多相关文章
- 【Leetcode_easy】1103. Distribute Candies to People
problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...
- 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 different k ...
- leetcode算法:Distribute Candies
Given an integer array with even length, where different numbers in this array represent different k ...
- [LeetCode] Distribute Candies 分糖果
Given an integer array with even length, where different numbers in this array represent different k ...
- [Swift]LeetCode575. 分糖果 | Distribute Candies
Given an integer array with even length, where different numbers in this array represent different k ...
- LeetCode 575 Distribute Candies 解题报告
题目要求 Given an integer array with even length, where different numbers in this array represent differ ...
- [LeetCode&Python] Problem 575. Distribute Candies
Given an integer array with even length, where different numbers in this array represent different k ...
- 575. Distribute Candies 平均分糖果,但要求种类最多
[抄题]: Given an integer array with even length, where different numbers in this array represent diffe ...
随机推荐
- 通过css控制超链接不显示下划线
“页面属性”——“链接”——“下划线样式”——“始终无下划线” <style type="text/css"> a:link { text-decoration: no ...
- HDMI转EDP芯片NCS8803简介
NCS8803 HDMI-to-eDP w/ Scaler Features --Embedded-DisplayPort (eDP) Output 1/2/4-lane eDP @ 1.62/2.7 ...
- js中bind、call、apply函数的用法 (转载)
最近看了一篇不错的有关js的文章,转载过来收藏先!!! 最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web 的项目,然后在腾讯实习的时候用 j ...
- JavaScript中事件
JS中的事件 一.事件分类: 鼠标事件:鼠标单击.鼠标双击.鼠标指上等... HTML事件:文档加载.焦点.表单提交等... 键盘事件:键盘按下(keydown).键盘按下并松开瞬间(keypress ...
- liunx下常见的命令汇总
前言:这篇文章对于工作多年的可能用处不大,但对于刚刚接触Java的同学肯定是有一些帮助,现在我总结我接触liunx后常见的一些命令 1:日志查询常用的命令 ll:查询目录下所有的文件 ls -lht: ...
- nginx正向代理
通过把Nginx设置为正向代理,我们就可以在局域网中用运行着Nginx的主机作为正向代理服务器了.那什么是正向代理和反向代理呢?正向代理和反向代理-百度百科 正向代理:如果把局域网外的Internet ...
- vue-cli搭建多页面项目如何配置
这里使用的是webpack模板. 首先安装vue-cli,执行命令 npm install vue-cli -g: 安装完成后初始化一个项目模板:vue init webpack myproject; ...
- poj 2063完全背包
题意:给出总资金和投资年份 ,n个股票 给出股票价格和其一年的利润.问如何选择能获得最大利润. 思路:股票可以重复选择,完全背包问题,完全背包也是从01背包衍生而行的,其主要区别在于中间那层循环的次序 ...
- Java运算符和流程控制
1 运算符 1.1 比较运算符 比较运算符的结果都是boolean类型,也即是要么是true,要么是false. 比较运算符"=="不能写成"=". > ...
- Coding使用方法
首先在码市coding.net上创建账号 基础配置 1. 首先,下载安装git客户端和tortoisegit(就是小乌龟,本地右键使用的,跟SVN一样的那个小乌龟). git下载官网:https:// ...