当前位置:网站首页>Leetcode- divide candy - simple
Leetcode- divide candy - simple
2022-06-13 05:50:00 【AnWenRen】
title : Divide candy - Simple
subject
Alice Yes n Piece sugar , Among them the first i The type of sugar is candyType[i] .Alice Notice that she's gaining weight , So I went to visit a doctor .
The doctor suggested Alice Eat less sugar , Just eat all her sugar n / 2 that will do (n It's an even number ).Alice I like these sweets very much , She wants to follow the doctor's advice , Eat as many different kinds of sugar as possible .
Give you a length of n Array of integers for candyType , return : Alice Eat only n / 2 In the case of sugar , The most kinds of sugar you can eat .
Example 1
Input :candyType = [1,1,2,2,3,3]
Output :3
explain :Alice Can only eat 6 / 2 = 3 Piece sugar , Because only 3 Seed sugar , She can eat one of each .
Example 2
Input :candyType = [1,1,2,3]
Output :2
explain :Alice Can only eat 4 / 2 = 2 Piece sugar , No matter what kind of food she chooses to eat [1,2]、[1,3] still [2,3], She can only eat two different kinds of sugar .
Example 3
Input :candyType = [6,6,6,6]
Output :1
explain :Alice Can only eat 4 / 2 = 2 Piece sugar , Although she can eat 2 gold , But you can only eat 1 Seed sugar .
Tips
n == candyType.length
2 <= n <= 104
n
It's an even number-105 <= candyType[i] <= 105
Code Java
public int distributeCandies(int[] candyType) {
// Set obtain Number of candy types
Set set = new HashSet();
for (int num : candyType) {
set.add(num);
}
// return Small value of both
return Math.min(set.size(), candyType.length / 2);
}
边栏推荐
- JS output uincode code
- Leetcode- intersection of two arrays ii- simple
- How to set the import / export template to global text format according to the framework = (solve the problem of scientific counting)
- AUTOSAR实战教程pdf版
- Explanation of sentinel series' features, composition and deployment
- Basic application of sentinel series
- Vagrant virtual machine installation, disk expansion and LAN access tutorial
- Zero copy technology
- Hump naming and underlining
- 中断处理过程
猜你喜欢
How to Algorithm Evaluation Methods
Working principle of sentinel series (concept)
Application virtual directory static resource configuration on tongweb
arrayList && linkedList
Interrupt processing
Concurrent programming -- source code analysis of thread pool
Why do so many people hate a-spice
890. Find and Replace Pattern
17 servicetask of flowable task
2021.9.30 learning log -postman
随机推荐
软件测试——接口常见问题汇总
Summary of the 11th week of sophomore year
Leetcode Hamming distance simple
Tongweb adapts to openrasp
Concurrent programming -- source code analysis of thread pool
Building a stand-alone version of Nacos series
SPI primary key generation strategy for shardingsphere JDBC
Unity game optimization (version 2) learning record 7
Explanation of sentinel series' features, composition and deployment
Leetcode- reverse string - simple
Input a number and output each digit from high to low
@Detailed explanation of propertysource usage method and operation principle mechanism
19 calling subprocess (callactivity) of a flowable task
powershell优化之一:提示符美化
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
Application virtual directory static resource configuration on tongweb
Leetcode- complement of numbers - simple
Unity游戏优化(第2版)学习记录7
Vagrant virtual machine installation, disk expansion and LAN access tutorial
arrayList && linkedList