INSERT INTERVAL | LEETCODE 57 | PYTHON SOLUTION
Vložit
- čas přidán 24. 07. 2024
- Channel Discord Community: / discord
Problem Link: leetcode.com/problems/insert-...
Today we are solving another interval based problem that combines some of the logic from the other ones: Insert Interval (LC # 57).
TIMESTAMPS:
00:00 Intro
00:08 Question Prompt
00:49 Examples
04:12 Solution Intuition
05:50 Coding
14:10 Time/Space Complexity
15:45 Outro - Věda a technologie
Tricky one to explain! I appreciate your efforts as always
if (!intervals.size()) return {newInterval};
int i = 0, n = intervals.size();
vector ans;
// condition 1 - continue to merge as long as there is no overlap
while (i < n && intervals[i][1] < newInterval[0]) {
ans.push_back(intervals[i]);
i++;
}
// condition 2 - overlap conflict - resolve by merging
while (i < n && intervals[i][0]
need to push the newInterval to ans before the last while loop
@@MadWe582 ahh yes you are correct.. this should be done after the 2nd while loop to insert the newly merged intervals.
I will not fix it in, but let whoever is reading this solution figure it out where to insert it :)
This ignores the requirement that you have to modify the intervals array in place and return it. You're not supposed to create a new array with the new intervals. I'm not saying that it won't pass, but it technically doesn't meet the requirements the problem is asking.
It literally says:
"Note that you don't need to modify intervals in-place. You can make a new array and return it."