Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

https://leetcode.

com/discuss/study-guide/2166045/line-sweep-algorithms

Why Sorting with endTimes Works


In many of the problem above we do sorting with endTime , this is an important concept to understand,
if you sort with endTime and then check other intervals you can easily find non-overlapping intervals like
this, here prev is previous interval.
if(intervals[i][0] < intervals[prev][1])

Reason is if a new interval start is before previous end time that means a sure overlap.
While on the other hand if we sort by startTime, we dont know when this interval gonna end, there
will be overlaps.
Here are some additional points to consider:

Sorting by end time is a greedy algorithm. This means that it makes the best possible choice at
each step, without considering the future. As a result, it is usually more efficient than sorting by start
time.
Sorting by start time is a dynamic programming algorithm. This means that it makes a choice at
each step, based on the choices that it has made in the past. As a result, it is usually more robust to
errors in the input data.
For practice try to solve this problem , which can be solved in both DP as well as Greey Way and see
the difference

Questions

You might also like