【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 ...
随机推荐
- LeetCode_83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List Easy Given a sorted linked list, delete all duplicates such t ...
- MongoDB集群之分片技术应用 —— 学习笔记
课程链接:https://www.imooc.com/learn/501 一.什么是分片? 分片:将数据进行2拆分,将数据水平的分散到不同的服务器上. 二.为什么要分片? 架构上:读写均衡.去中心化 ...
- sersync参数说明
-v, --verbose 详细模式输出-q, --quiet 精简输出模式-c, --checksum 打开校验开关,强制对文件传输进行校验-a, --archive 归档模式,表示以递归方式传输文 ...
- 图形学入门(3)——区域填充算法(region filling)
继续图形学之旅,我们已经解决了如何画线和画圆的问题,接下来要解决的是,如何往一个区域内填充颜色?对一个像素填充颜色只需调用SetPixel之类的函数就行了,所以这个问题其实就是:如何找到一个区域内的所 ...
- php 因循环数据 赋值变量 占用内存太大 提示错误
Fatal error: Allowed memory size of 134217728 bytes exhausted 网上很多解决方法:就简单记录下 一个csv导入功能 由于数据太多 占用内存太 ...
- React native 禁止手势触摸 pointerEvents
碰到一个需求是做个轮播图带自动播放的,UED那边给轮播的底部加了阴影渐变,如下红色区域. 这样会导致一个问题,手触摸在红色区域会被这层View挡住,导致不能手动滑动切换. 原先采取过的方法是在对应的触 ...
- python 脚本备份mssql数据库并删除数据库
一.实现脚本 # -*- coding=utf-8 -*- import pyodbc from datetime import datetime import pymssql import os i ...
- Python基础运算符(算数、比较、赋值、逻辑、成员)
Python运算符有(算数运算符.比较运算符.赋值运算符.逻辑运算符.位运算符.成员运算符.身份运算符): 本程序包含算数.比较.赋值.逻辑.成员运算符. 1.运算符测试 #!/usr/bin/pyt ...
- 『Python基础练习题』day01
个人主页: https://lipeiguan.top 1.简述变量命名规范 2.name = input(">>>") name变量是什么数据类型? 3.if条 ...
- PyCryptodome安装使用方法
PyCryptodome是PyCrypto的一个分支.基于PyCrypto2.6.1,多了以下特性: Authenticated encryption modes (GCM, CCM, EAX, SI ...