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

How to resize an image in C #

Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)

Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"

Technology blog | how to communicate using SSE

机器学习笔记 - 时间序列的线性回归

基于STM32MP157调试MIPI-DSI屏幕

OAuth 2.0一键登录那些事

神经网络与深度学习-3- 机器学习简单示例-PyTorch

OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线

传统的IO存在什么问题?为什么引入零拷贝的?
随机推荐
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
Requirements for Power PCB circuit board design 2021-11-09
一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
Audio (V) audio feature extraction
How to select lead-free and lead-free tin spraying for PCB? 2021-11-16
What are the benefits of reserving process edges for PCB production? 2021-10-25
PCB board design - automatic layout 2021-10-15
國外LEAD域名郵箱獲取途徑
FairMOT yolov5s转onnx
Five causes of PCB board deformation and six solutions 2021-10-08
How to resize an image in C #
[little knowledge] PCB proofing process
微信小程序开通客服消息功能开发
Basic use of ActiveMQ in Message Oriented Middleware
NSIS silent installation vs2013 runtime
Force deduction 76 questions, minimum covering string
Runtime - Methods member variable, cache member variable
@Resource和@Autowired注解的不同,为什么推荐@Resource?
双三次差值bicubic
STL tutorial 4- input / output stream and object serialization