MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

LeetCode_problems/Insert Interval/solution.cpp:31–64  ·  view source on GitHub ↗

AUTHOR: github.com/Sanjay235 LOGIC: ========================================================================================== 1. As the set of intervals provided are non-overlapping and sorted, we don't need to do additional sorting for ease of merge. 2. First traverse all intervals in the set for which end point is less than start point of new interval and reach an overlapping interval with

Source from the content-addressed store, hash-verified

29==========================================================================================
30*/
31class Solution {
32public:
33 vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
34 vector<vector<int>> res;
35
36 int index = 0;
37
38 // Step - 2
39 while(index < intervals.size() && intervals[index][1] < newInterval[0]){
40 res.push_back(intervals[index++]);
41 }
42
43 // Step - 3
44 while(index < intervals.size() && intervals[index][0] <= newInterval[1]){
45 // Step - 3 (a)
46 newInterval[0] = min(newInterval[0], intervals[index][0]);
47
48 // Step - 3 (b)
49 newInterval[1] = max(newInterval[1], intervals[index][1]);
50
51 index++;
52 }
53
54 // Step - 4
55 res.push_back(newInterval);
56
57 // Step - 5
58 while(index < intervals.size()){
59 res.push_back(intervals[index++]);
60 }
61
62 return res;
63 }
64};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected