【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 ...
随机推荐
- python基础之坑爹正则表达式
python基础之坑爹正则表达式 概述 re模块就是python语言中的正则表达式,拆出来单独写一条blog是因为正则表达式本身就是比较庞大的知识,写具体些让自己以后方便查找. IP: ^(25[0- ...
- Kubernetes简介二
Kubernetes是什么?Kubernetes是容器集群管理系统,是一个开源的平台,可以实现容器集群的自动化部署.自动扩缩容.维护等功能.通过Kubernetes你可以: 快速部署应用 快速扩展应用 ...
- 【MM系列】SAP S/4 HANA的物料编码40位设置
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP S/4 HANA的物料编码4 ...
- netcore mvc菜单,角色,权限
netcore mvc快速开发系统(菜单,角色,权限[精确到按钮])开源 AntMgr https://github.com/yuzd/AntMgr 基于netcore2.0 mvc 开发的 快速搭建 ...
- Jenkins+MSbuild+SVN实现快速搭建.net持续集成环境(构建、编辑、部署到服务器)
Jenkins是一个可扩展的持续集成引擎,Jenkins非常易于安装和配置,简单易用,下面开始搭建.net持续集成环境 Jenkins和SVN安装这里就不介绍了 一.准备工作 1.Jenkins中系统 ...
- jqGrid刷新不取消选中
也就是说,刷新前选中的行,刷新后还是选中 var jqGrid = $("#jqGrid"); // 获取刷新前选中的行id var jqGridRowid=jqGrid.jqGr ...
- golang json解析到map中
package main import ( "fmt" "encoding/json" ) type ItemMessage struct { ItemType ...
- Java面试题代码篇
1.统计字符串中的各种字符的个数并对其排序 package JavaMianSiTest; public class TongJIZiFu { public static void main(Stri ...
- Java基础---Java循环区别
三种循环的区别. 1. 如果条件判断从来没有满足过,那么for循环和while循环将会执行0次,但是do-while循环会执行至少一次.2. for循环的变量在小括号当中定义,只有循环内部才可以使用. ...
- 脉脉的一道网红Java面试题
题目如下: public class Test { public static void main(String[] args) { int a = 10; int b = 10; // 需要在met ...