当前位置:网站首页>1854. 人口最多的年份
1854. 人口最多的年份
2022-06-11 09:06:00 【Mr Gao】
1854. 人口最多的年份
给你一个二维整数数组 logs ,其中每个 logs[i] = [birthi, deathi] 表示第 i 个人的出生和死亡年份。
年份 x 的 人口 定义为这一年期间活着的人的数目。第 i 个人被计入年份 x 的人口需要满足:x 在闭区间 [birthi, deathi - 1] 内。注意,人不应当计入他们死亡当年的人口中。
返回 人口最多 且 最早 的年份。
示例 1:
输入:logs = [[1993,1999],[2000,2010]]
输出:1993
解释:人口最多为 1 ,而 1993 是人口为 1 的最早年份。
示例 2:
输入:logs = [[1950,1961],[1960,1971],[1970,1981]]
输出:1960
解释:
人口最多为 2 ,分别出现在 1960 和 1970 。
其中最早年份是 1960 。
这题其实比较简单,我们可以很清楚的发现,最优解肯定是在端点处,而且是区间的左端点,遍历一下,就可以求出结果
解题代码如下:
int maximumPopulation(int** logs, int logsSize, int* logsColSize){
int max;
int time=logs[0][0];
max=1;
int j;
int i;
int t=logs[0][0];
for(i=0;i<logsSize;i++){
t=logs[i][0];
int num=0;
for(j=0;j<logsSize;j++){
if(t>=logs[j][0]&&t<=logs[j][1]-1){
num++;
}
}
if(num==max){
max=num;
if(time>t){
time=t;
}
}
if(num>max){
max=num;
time=t;
}
}
return time;
}
边栏推荐
- Complexity analysis of matrix inversion operation (complexity analysis of inverse matrix)
- SAP ABAP field symbol
- Matlab r2022a installation tutorial
- File system check of the root filesystem failed
- Textview text size auto fit and textview margin removal
- php 上传大文件 超过40秒 服务器500
- C language printing heart
- Flutter development log - route management
- leveldb简单使用样例
- 2130. maximum twin sum of linked list
猜你喜欢

CUMT learning diary - theoretical analysis of uCOSII - Textbook of Renzhe Edition

SAP OData development tutorial

Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC

Which Apple devices support this system update? See if your old apple device supports the latest system

机器学习笔记 - 深度学习技巧备忘清单

C language printing diamond

Screening frog log file analyzer Chinese version installation tutorial

Textview text size auto fit and textview margin removal
![[image processing] spatial domain image enhancement](/img/d9/6afee1006570651ccf93ed8da95dd3.png)
[image processing] spatial domain image enhancement

Typescript -- preliminary study of variable declaration
随机推荐
openstack详解(二十二)——Neutron插件配置
SAP abap 字段符号
Sword finger offer II 036 Postfix Expression
Console you don't know
面试题 02.02. 返回倒数第 k 个节点
PVC plastic sheets BS 476-6 determination of flame propagation properties
Create a nodejs based background service using express+mysql
【C语言-数据存储】数据在内存中是怎样存储的?
876. 链表的中间结点
2095. 删除链表的中间节点
2130. maximum twin sum of linked list
【软件】ERP体系价值最大化的十点技巧
844. 比较含退格的字符串
小型制氧机解决方案PCBA电路板开发
SQL basic query
SAP ABAP field symbol
ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用
Exclusive interview with PMC member Liu Yu: female leadership in Apache pulsar community
Erreur de démarrage MySQL "BIND on TCP / IP Port: Address already in use"
SAP abap 数据类型与数据对象