当前位置:网站首页>Li Kou daily question - day 29 -575 Divide candy
Li Kou daily question - day 29 -575 Divide candy
2022-06-28 03:37:00 【Chongyou research Sen】
2022.6.27 Did you brush the questions today ?
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 , You can eat sugar most Number of species .
analysis :
Give you an even array , You need to find a value that is half the size of the array x, And the types of different elements y Compare , If x Greater than or equal to y, Then return to y, Otherwise return to x.
Ideas : We make use of map Container inserts elements , And then according to map The result can be obtained by comparing the size of the array with half the size of the original array
analysis :
1. Hashtable
class Solution {
public:
int distributeCandies(vector<int>& candyType) {
unordered_map<int, int>map;
int n = candyType.size() / 2;
int res = 0;
for (auto num : candyType)
{
map[num]++;
}
int m = map.size();
if (n <= m)
{
res = n;
}
else
{
res = m;
}
return res;
}
};
2.set Containers
Because we don't consider repeating elements , You can deposit it directly set In the container , And insert set Optimized for . because map You need to insert a key value and a key value pair at the same time, so you can't do this .
class Solution {
public:
int distributeCandies(vector<int>& candyType) {
unordered_set<int>set(candyType.begin(), candyType.end());
int s = set.size();
int res = min(s, (int)candyType.size() / 2);
}
};
边栏推荐
- How to write concise code? (upper)
- 自用工具 猴子都会用的unity视频播放器
- Importer un fichier Excel, résoudre le problème de sauter les cellules vides et de ne pas lire, et avancer l'indice, et retourner Blank As NULL Red
- Custom controls under WPF and adaption of controls in Grid
- 可扩展数据库(下)
- Establishment of SSH Framework (Part I)
- 资源管理、高可用与自动化(中)
- 剑指 Offer 49. 丑数(三指针法)
- 导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
- 劲爆!YOLOv6又快又准的目标检测框架开源啦(附源代码下载)
猜你喜欢
Go speed
Importer un fichier Excel, résoudre le problème de sauter les cellules vides et de ne pas lire, et avancer l'indice, et retourner Blank As NULL Red
可扩展数据库(上)
爱普生L3153打印机如何清洗喷头
Necessary software tools in embedded software development
Anaconda命令用法
Summary of the use of composition API in the project
根据Explain查看sql执行计划,对SQL进行优化
collections.defaultdict()的使用
Object类,以及__new__,__init__,__setattr__,__dict__
随机推荐
Resource management, high availability and automation (medium)
Arm development studio build compilation error
Brief history and future trend of codeless software
自用工具 猴子都会用的unity视频播放器
JS clear the object and its value:
What is the difference between slice and array in go question bank 12?
17 `bs object Node name h3 Parent ` parents get parent node ancestor node
crond BAD FILE MODE /etc/cron. d
__getitem__和__setitem__
2022年R1快開門式壓力容器操作特種作業證考試題庫及答案
剑指 Offer 53 - I. 在排序数组中查找数字 I(改进二分)
如何获取GC(垃圾回收器)的STW(暂停)时间?
学习---有用的资源
力扣每日一题-第29天-1491.去掉最低工资和最高工资后的平均工资
大咖说·计算讲谈社|什么是东数西算要解决的核心问题?
可扩展数据库(下)
CI & CD 不可不知!
SSH框架的搭建(上)
建立自己的网站(17)
Custom controls under WPF and adaption of controls in Grid