One Method to Solve Three Interval Problems on LeetCode
Note
Now all the plugins has supported English. I'm still improving the website...
This article will resolve
LeetCode | Difficulty |
---|---|
1288. Remove Covered Intervals | 🟠 |
56. Merge Intervals | 🟠 |
986. Interval List Intersections | 🟠 |
Many readers often ask about interval-related problems. Today, I'm writing an article to tackle three such problems efficiently.
Interval problems are essentially segment problems, where you need to merge all segments, find their intersections, etc. There are two main techniques:
1. Sorting. The common approach is to sort by the starting point of the intervals. Alternatively, you can sort by the starting point in ascending order and, if the starting points are the same, sort by the ending point in descending order. Of course, if you prefer to sort by the ending point, it's just a symmetric operation; the essence remains the same.
2. Drawing. This means don't be lazy; be hands-on. Understand the different possible relative positions of two intervals and how your code should handle each scenario.
Enough talk, let's dive into the problems.