当前位置:网站首页>57. insert interval
57. insert interval
2022-06-25 07:52:00 【Mr Gao】
57. Insert interval
To give you one Without overlap , A list of intervals sorted by their starting and ending points .
Insert a new interval in the list , You need to make sure that the intervals in the list are still ordered and do not overlap ( If necessary , You can merge ranges ).
Example 1:
Input :intervals = [[1,3],[6,9]], newInterval = [2,5]
Output :[[1,5],[6,9]]
Example 2:
Input :intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output :[[1,2],[3,10],[12,16]]
explain : This is because of the new range [4,8] And [3,5],[6,7],[8,10] overlap .
Example 3:
Input :intervals = [], newInterval = [5,7]
Output :[[5,7]]
Example 4:
Input :intervals = [[1,5]], newInterval = [2,3]
Output :[[1,5]]
Example 5:
Input :intervals = [[1,5]], newInterval = [2,7]
Output :[[1,7]]
int** insert(int** intervals, int intervalsSize, int* intervalsColSize, int* newInterval, int newIntervalSize, int* returnSize, int** returnColumnSizes) {
*returnSize = 0;
int left = newInterval[0];
int right = newInterval[1];
bool placed = false;
int** ans = malloc(sizeof(int*) * (intervalsSize + 1));
*returnColumnSizes = malloc(sizeof(int*) * (intervalsSize + 1));
for (int i = 0; i < intervalsSize; ++i) {
int* interval = intervals[i];
if (interval[0] > right) {
// On the right side of the insertion interval and without intersection
if (!placed) {
int* tmp = malloc(sizeof(int) * 2);
tmp[0] = left, tmp[1] = right;
(*returnColumnSizes)[*returnSize] = 2;
ans[(*returnSize)++] = tmp;
placed = true;
}
int* tmp = malloc(sizeof(int) * 2);
memcpy(tmp, interval, sizeof(int) * 2);
(*returnColumnSizes)[*returnSize] = 2;
ans[(*returnSize)++] = tmp;
} else if (interval[1] < left) {
// On the left side of the insertion interval and without intersection
int* tmp = malloc(sizeof(int) * 2);
memcpy(tmp, interval, sizeof(int) * 2);
(*returnColumnSizes)[*returnSize] = 2;
ans[(*returnSize)++] = tmp;
} else {
// There is an intersection with the insertion interval , Compute their union
left = fmin(left, interval[0]);
right = fmax(right, interval[1]);
}
}
if (!placed) {
int* tmp = malloc(sizeof(int) * 2);
tmp[0] = left, tmp[1] = right;
(*returnColumnSizes)[*returnSize] = 2;
ans[(*returnSize)++] = tmp;
}
return ans;
}
边栏推荐
- C control refresh
- How to use printf of 51 single chip microcomputer
- AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
- Different paths ii[dynamic planning improvement for DFS]
- C# 读取web上的xml
- Summary of small problems in smartbugs installation
- Vscode is good, but I won't use it again
- 环网冗余式CAN/光纤转换器的CAN光端机在消防火灾联网报警系统中的应用
- VOCALOID笔记
- NSIS silent installation vs2013 runtime
猜你喜欢

差点被这波Handler 面试连环炮带走~

Do you know why the PCB produces tin beads? 2021-09-30

基于STM32MP157调试MIPI-DSI屏幕

Modular programming of oled12864 display controlled by single chip microcomputer

Keil and Proteus joint commissioning

How to resize an image in C #

海思3559 sample解析:vio

Terms and concepts related to authority and authentication system

使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障

年后求职找B端产品经理?差点把自己坑惨了......
随机推荐
权限、认证系统相关名词概念
力扣76题,最小覆盖字串
npm install 报错 : gyp ERR! configure error
Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
Function template_ Class template
Fairmot yolov5s to onnx
27. 移除元素
Five causes of PCB board deformation and six solutions 2021-10-08
Misunderstanding of switching triode
个人域名和企业域名的区别
"Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
SCM Project Training
How to use ad wiring for PCB design?
Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134 vulnerability analysis and protection
Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
1742. 盒子中小球的最大数量
国外LEAD域名邮箱获取途径
C reads XML on the web
传统的IO存在什么问题?为什么引入零拷贝的?