Merge Intervals (LeetCode 56): sort first, then one sweep, animated
Systems in Ink
0:00 / 0:00
Merge Intervals (LeetCode 56): sort first, then one sweep, animated
76 просмотров · 4 дн. назад
Systems in Ink
808 подписчиков
76 просмотров · 4 дн. назад
Merge Intervals is LeetCode problem 56, on four of the five big public interview lists. This video explains it from scratch with our own example, seven meeting slots that squash into three busy blocks: why comparing every pair is about fifty million checks at ten thousand slots, the one idea that makes a single sweep enough (sort by start time first), why that works (everything before the last block is final, so only the last block can still grow), and the real Python code running step by step. Every bar, block and number on screen is read from the real run of the tested solution. Plus the traps: writing the new end instead of the larger end, skipping the sort, and a strict test that leaves touching slots apart, and the follow-ups interviewers ask (already sorted input, free time, meeting rooms).
Problem: LeetCode 56, Merge Intervals (Medium) · https://leetcode.com/problems/merge-i...
Pattern: Intervals · On: NeetCode 150, Blind 75, Grind 169, LeetCode Top Interview 150
Chapters
0:00 Cold open
0:19 The problem
0:50 The obvious answer
1:19 The trick
2:09 Dry run
3:28 The code
4:06 Cost and traps
4:59 Follow-ups
5:30 The pattern
Cost: O(n log n) time, O(n) extra memory.
Sources
• LeetCode 56: Merge Intervals: https://leetcode.com/problems/merge-i...
• NeetCode 150: https://neetcode.io/practice
• Blind 75: https://neetcode.io/practice
• Grind 169: https://www.techinterviewhandbook.org...
• LeetCode Top Interview 150: https://leetcode.com/studyplan/top-in...
• The sort-then-sweep trick behind merge intervals (techinterview.org): https://www.techinterview.org/post/32...
• Interval problem patterns: merge, insert, meeting rooms (techinterview.org): https://www.techinterview.org/post/32...
• Python sorting HOWTO: https://docs.python.org/3/howto/sorti...
The problem is explained in our own words with our own example. Every state in the animation comes from running the solution shown, and the code is tested (plus a random cross-check against the brute force) before the video is made.
#LeetCode #MergeIntervals #CodingInterview #DSA #Python