当前位置:网站首页>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);
}
};边栏推荐
- STM32外设SDIO和SD卡的配置
- crond BAD FILE MODE /etc/cron. d
- What is the difference between slice and array in go question bank 12?
- R1 Quick Open Pressure Vessel Operation Special Operation Certificate Examination Library and Answers in 2022
- 同样是MB,差距怎么这么大呢?
- Basic operation of stack (implemented in C language)
- 2022危险化学品经营单位安全管理人员特种作业证考试题库模拟考试平台操作
- 2022电工(初级)复训题库及在线模拟考试
- TypeError:&nbsp;&#039;module&amp;#03…
- CI & CD 不可不知!
猜你喜欢

17 `bs对象.节点名h3.parent` parents 获取父节点 祖先节点

Question bank and answers of special operation certificate for R1 quick opening pressure vessel operation in 2022

开口式霍尔电流传感器如何助力直流配电改造?

__getitem__和__setitem__

「运维有小邓」监控文件及文件夹变更

Build your own website (17)

Set drop-down options on Excel files

導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅

s32ds跳转到DefaultISR

Excel知识技能汇总
随机推荐
JS clear the object and its value:
Notepad++--列编辑模式--用法/实例
The same is MB. Why is the gap so large?
2022年R1快開門式壓力容器操作特種作業證考試題庫及答案
在excel文件上设置下拉选项
INFO:&nbsp;HHH000397:&nbsp;Using…
2022年R1快开门式压力容器操作特种作业证考试题库及答案
可扩展数据库(下)
剑指 Offer 47. 礼物的最大价值(DP)
数据库系列之InnoDB中在线DDL实现机制
Ten years' experience of Software Engineer
No&nbsp;result&nbsp;defined&amp;nbsp…
How does the open-ended Hall current sensor help the transformation of DC power distribution?
TypeScript 联合类型
十年职场软件工程师感悟
2022安全员-C证考试题库模拟考试平台操作
Hot! Yolov6's fast and accurate target detection framework is open source (with source code download)
TypeError:&nbsp;&#039;module&amp;#03…
2022 electrician (elementary) recurrent training question bank and online simulation examination
爱普生L3153打印机如何清洗喷头