【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 ...
随机推荐
- matlab学习——02整数规划(蒙特卡洛法,指派问题,混合整数规划)
02整数规划 蒙特卡洛法(随机取样法) 编写文件mengte.m,目标函数f和约束向量g function[f,g]=mengte(x); f=x(1)^2+x(2)^2+3*x(3)^2+4*x(4 ...
- ABAP编辑器输入中文变成问号
在ABAP编辑器里输入汉字,点击空格后显示问号? 中英文环境下编辑都出现乱码 实用程序->设置 ->基于文本的编辑器 如果用老式编辑器,可以输入中文 试试打个补丁 GUI740 补丁17 ...
- stub_status监控Nginx使用情况!
stub_status监控Nginx使用情况! 查看nginx安装的模块! # /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.8.1 bu ...
- S2. Android 常用控件
[概述] Button(普通按钮):点击事件处理 Toast(消息提示) Menu(菜单): Menu + Fragment 实现菜单切换 [Button] 在 MainActivity 对应的布局文 ...
- todo---ezmorph
todo---ezmorph
- C++中静态成员函数和普通成员函数存储方式相同
先从一个示例查看类的创建过程中,静态成员函数和普通成员函数的存储区别. #include "stdafx.h" #include<iostream> #include& ...
- 关联安装 mysql ,zabbix , nginx, php
/usr/local/zabbix-3.2.6 /usr/local/php-5.6.3 /usr/local/mysql-5.7.26 安装mysql mv /etc/yum.repos.d/Cen ...
- Linux驱动函数解读
一.kmalloc().kzalloc()和vmalloc() 这三个函数都可以分配连续的虚拟内存 除此之外,这三个函数的区别有: 1. kmalloc()和kzalloc()函数分配的物理内存也是连 ...
- 『Python基础』第1节 Windows环境下安装Python3.x
一. Python安装 1. 下载安装包 https://www.python.org/downloads/release/python-374/ # 3.7安装包 # 如需安装python2.7版本 ...
- mac OS 安装qt环境
先安装xcode,然后从官网下载dmg安装包安装即可,这里主要出现一个问题就是创建的工程没法编译,提示没有构建包(kit) 要安装command line tool才行. 在命令行中输入: xcode ...