当前位置:网站首页>16.10. Number of survivors
16.10. Number of survivors
2022-07-20 11:36:00 【soO_ 007】
subject :
Given N The year of birth and the year of death of the individual , The first i The year of birth of the individual is birth[i], The year of death was death[i], Implement a method to calculate the year with the largest number of survivors .
You can assume that everyone was born in 1900 - 2000 year ( contain 1900 and 2000 ) Between . If a person is living at any time of the year , Then he should be included in the statistics of that year . for example , Born in 1908 year 、 Die of 1909 People in should be included in 1908 Years and 1909 Year count .
If the number of survivors is the same in multiple years and all are the maximum , Output the smallest year .
Example :
Input :
birth = {1900, 1901, 1950}
death = {1948, 1951, 2000}
Output : 1901
Tips :
0 < birth.length == death.length <= 10000
birth[i] <= death[i]
Ideas 1:
Maintenance interval , First, save the time points of life and death in order , At the same time, the year and number of life and death are hashed map Save it . Initialize three int,cur Represents the current number ,big Represents the largest number of people so far ,year Record the year . Traverse time point , If there is a birth at the current time , It's in cur add , And with big contrast , If cur Bigger , to update big And year. Then we'll see if there was death, If so, start from cur subtract . because death People existed in those days , Only in the next year “ Die completely ”, So from death Update complete cur Don't talk with big Compare .
Code 1:
class Solution {
public:
int maxAliveYear(vector<int>& birth, vector<int>& death) {
unordered_map<int, int> b, d;
set<int> time;
for (auto i : birth) {
time.insert(i);
b[i]++;
}
for (auto i : death) {
time.insert(i);
d[i]++;
}
int cur = 0, big = 0, ans = birth[0];
for (auto t : time) {
if (b.count(t)) {
cur += b[t];
if (cur > big) {
big = cur;
ans = t;
}
}
if (d.count(t)) {
cur -= d[t];
}
}
return ans;
}
};
Ideas 2:
Array difference , The principle is to modify at the current time point when life or death occurs , The prefix sum of each subsequent year is the number of people in that year . The first year is [1900, 2000], Both sides include , therefore index Will arrive 101, That is, the array needs to be initialized 102. then death People don't count in that year , When the deposit reaches the time point, it should be postponed for a year , Remember death + 1 In the year . Finally, traverse to get the prefix and the maximum year .
Code 2:
class Solution {
public:
int maxAliveYear(vector<int>& birth, vector<int>& death) {
vector<int> time(102);
for (int i = 0; i < birth.size(); i++) {
time[birth[i] - 1900]++;
time[death[i] - 1900 + 1]--;
}
int year = 0, ans = 0, cur = 0;
for (int i = 0; i < 102; i++) {
cur += time[i];
if (cur > ans) {
ans = cur;
year = i;
}
}
return year + 1900;
}
};
边栏推荐
- 數據庫基礎知識
- 导电滑环的内部结构和使用范围
- PriorityQueue——优先级队列(堆)
- Factors affecting the quality of slip rings in production
- How to import data from the client into Youxuan database through the copy command
- C4 learning materials (to be continued)
- NLP pre training model for knowledge enhancement [introducing triplet vectors in the knowledge map into the pre training model]
- LeetCode 第 302 场周赛
- View the intranet mapping between host IP port and fast resolution
- 一文看懂25个神经网络模型 - 人工神经网络的典型模型
猜你喜欢
随机推荐
215位“双一流”考生考研,被这所“双非大学”录取!
Presto SQL 查询时可以实现动态表名吗?
低频量化之可转债埋伏配债、埋伏埋伏配债和配债选股策略
第五天笔记整理
MGRE环境下的OSPF实验
redis 数据类型和使用场景
2022爱分析・银行数字化实践报告
Design and software implementation of FIR digital filter in Digital Signal Processing Experiment III
884. Search arrangement
C#本质论 泛型
微信小程序手动实现watch监听
Hash table (hash table) and hash conflict
yum install 报警 No package xxxx available
Redis e-commerce spike design
中文同义句在线转换器 - 中文同义句转换器软件
leetcode-11 盛水最多的容器(双指针,lower_bound, upperbound)
2022p cylinder filling examination question bank and answers
快解析结合绿盾文档加密软件
Digital signal processing experiment II IIR digital filter design and software implementation
MySQL调优待完善