Best Solution in C++ 100% Easily Understandable
Approach\nExplained in the code using comments\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity: O(NlogN)\n Add your time co
# Approach\nExplained in the code using comments\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(NlogN)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {\n        vector<vector<int>> ans;\n\n        //sort using the same logic as Merge Intervals question\n        sort(intervals.begin(), intervals.end()); \n\n        for(auto it: intervals)\n        {\n            //if interval before is smaller add that\n            if(it[1] < newInterval[0]) \n                ans.push_back(it);\n\n            //if the newInterval is smaller add that\n            else if(newInterval[1] < it[0]) \n            {\n                ans.push_back(newInterval);\n                newInterval = it;\n            }\n            \n            //if newInterval lies within the currentIntervals\n            else \n            {\n                newInterval[0] = min(newInterval[0], it[0]);\n                newInterval[1] = max(newInterval[1], it[1]);\n            } \n        }\n\n        //add the last Interval\n        ans.push_back(newInterval);\n\n        return ans;                                                                          \n    }\n};\n```\n\n![pleaseupvotye.jpeg](https://assets.leetcode.com/users/images/e1cd0d4f-0255-4afa-8f6e-b5fff843647b_1693664013.5913265.jpeg)\n
['C++']