当前位置:网站首页>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;
}
边栏推荐
- OAuth 2.0一键登录那些事
- [daily training] 207 Class Schedule Card
- LeetCode_哈希表_中等_454.四数相加 II
- SSL证书免费获取教程
- 双三次差值bicubic
- Usememo simulation usecallback
- What are the benefits of reserving process edges for PCB production? 2021-10-25
- @The difference between resource and @autowired annotation, why recommend @resource?
- 搞清信息化是什么,让企业转型升级走上正确的道路
- 【视频】ffplay 使用mjpeg格式播放usb摄像头
猜你喜欢
机器学习笔记 - 时间序列的线性回归
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
Home environment monitoring system design (PC version) (mobile app version to be determined)
Modular programming of oled12864 display controlled by single chip microcomputer
Estimation of dense forest volume based on LIDAR point cloud with few ground points
El input to add words to the tail
基于RBAC 的SAAS系统权限设计
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
How to use ad wiring for PCB design?
Misunderstanding of switching triode
随机推荐
NSIS silent installation vs2013 runtime
MySQL简单权限管理
test
OAuth 2.0 one click login
新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
Modular programming of LCD1602 LCD controlled by single chip microcomputer
Microsoft Office Word 远程命令执行漏洞(CVE-2022-30190)分析与利用
Take you through the normalization flow of GaN
力扣78:子集
What are the problems with traditional IO? Why is zero copy introduced?
OpenMP入门
npm install 报错 : gyp ERR! configure error
Home environment monitoring system design (PC version) (mobile app version to be determined)
Getting started with OpenMP
差点被这波Handler 面试连环炮带走~
Kinsing双平台挖矿家族病毒分析
基于Anaconda的模块安装与注意事项
How to use ad wiring for PCB design?
C get the version number of exe - file version and assembly version
Force deduction 76 questions, minimum covering string