博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 575. Distribute Candies
阅读量:6657 次
发布时间:2019-06-25

本文共 1427 字,大约阅读时间需要 4 分钟。

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: 2Explanation: 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:
  1. The length of the given array is in range [2, 10,000], and will be even.
  2. The number in given array is in range [-100,000, 100,000].

Solution:

先去重,sister最多拿到candies的一半。

/** * @param {number[]} candies * @return {number} */var distributeCandies = function(candies) {    let temp = [];    candies.forEach(function(candie) {        if(temp.indexOf(candie) == -1) {            temp.push(candie);        }    });    if(temp.length >= candies.length / 2) {        return candies.length /2 ;    } else {        return temp.length;    }};

 

转载于:https://www.cnblogs.com/gogolee/p/7504596.html

你可能感兴趣的文章
JAVA并发处理经验(四)并行模式与算法5:并行排序模式-奇偶性排序
查看>>
html常见兼容性问题?
查看>>
Jquery封装tab选项卡
查看>>
gitlab修改项目的url
查看>>
完全用链表实现的贪吃蛇
查看>>
平板不会取代办公PC 但某种技术可能会
查看>>
理解javascript面向对象的基本模式
查看>>
体验无人值守安装RHEL6
查看>>
Unable to access ossec directory的解决
查看>>
super daemon与tcp_wrappers结合应用小结(实验部分)
查看>>
nginx + uwsgi + Django 应用部署
查看>>
乾颐堂军哥HCIE12-BGP的对等体组和BGP的路由操控理论和实验
查看>>
windows中取消活动分区状态
查看>>
NTP 时间同步服务器配置实例(CISCO 7200路由)
查看>>
文件查找工具find
查看>>
mooon-agent设计要点
查看>>
解决 IE 9 Beta 频繁崩溃的问题
查看>>
数据库设计中的14个技巧
查看>>
MySQL深入02-DML之Select查询
查看>>
取n到m条记录的语句
查看>>