当前位置:网站首页>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);
}
};边栏推荐
猜你喜欢

第二轮红队免费公开课来袭~明晚八点!

Solution to not displaying logcat logs during debugging of glory V8 real machine

What is the core problem to be solved in the East and West?

2022年R1快开门式压力容器操作特种作业证考试题库及答案

基于 LNMP 搭建个人网站的填坑之旅

Inference optimization implementation of tensorrt model

Set drop-down options on Excel files

Resource management, high availability and automation (medium)

Is your IOT security strong enough?

2022危险化学品经营单位安全管理人员特种作业证考试题库模拟考试平台操作
随机推荐
数的每一位平方和
MySQL 数据库的自动备份操作
Tardigrade: Trino's solution to ETL scenarios
R1 Quick Open Pressure Vessel Operation Special Operation Certificate Examination Library and Answers in 2022
云应用、服务的“5层”架构
栈的基本操作(C语言实现)
启牛商学院赠送证券账户是真的吗?开户到底安不安全呢
Resource management, high availability and automation (medium)
Question bank and answers of special operation certificate for R1 quick opening pressure vessel operation in 2022
INFO:&nbsp;HHH000397:&nbsp;Using…
service实现类里面为何一直报红
Is it safe to buy stocks and open an account through the account opening link of the broker manager? Want to open an account for stock trading
Etcd database source code analysis -- network layer server rafthandler between clusters
Ten years' experience of Software Engineer
xml&nbsp;文件的读写
STM32外设SDIO和SD卡的配置
剑指 Offer 49. 丑数(三指针法)
可扩展数据库(下)
Solution to not displaying logcat logs during debugging of glory V8 real machine
文档问题