Non-overlapping Intervals

Greedy Algorithms Scheduling and Interval Problems Medium
  • This type of problem scenario and its underlying concept is widely used in the area of resource scheduling in the software industry
  • For instance, in cloud computing, there can be multiple requests for a resource within overlapping intervals
  • Solving a problem like this could determine the minimum number of requests to be canceled, moved or rescheduled to ensure that resource allocation does not overlap, thereby optimizing resource utilization
  • Another popular use case is in the organization of tasks or events in calendar apps where the goal is minimizing event overlap to free up time slots

Given an array of N intervals in the form of (start[i], end[i]), where start[i] is the starting point of the interval and end[i] is the ending point of the interval, return the minimum number of intervals that need to be removed to make the remaining intervals non-overlapping.

Examples:

Input : Intervals = [ [1, 2] , [2, 3] , [3, 4] ,[1, 3] ]


Output : 1


Explanation : You can remove the interval [1, 3] to make the remaining interval non overlapping.

Input : Intervals = [ [1, 3] , [1, 4] , [3, 5] , [3, 4] , [4, 5] ]


Output : 2


Explanation : You can remove the intervals [1, 4] and [3, 5] and the remaining intervals becomes non overlapping.

Input : Intervals = [ [1, 10] , [1, 4] , [3, 8] , [3, 4] , [4, 5] ]

Constraints

  • 1 <= Intervals.length <= 105
  • 0 <= start[i] < end[i] <= 105
  • Intervals[i].length = 2

Hints

  • Sort the intervals based on their end values in ascending order. If two intervals have the same end, sort by their start. After sorting, initialize a variable to keep track of the end time of the last selected interval.
  • Traverse the intervals. If the start time of the current interval is greater than or equal to the end time of the last selected interval, keep the current interval and update the end time. If the intervals overlap (i.e., the start time is less than the current end time), increment the count of intervals to be removed.

Company Tags

Rockstar Games NVIDIA Seagate Technology Docker Goldman Sachs Freshworks Salesforce American Express Red Hat Bungie Morgan Stanley Nutanix Swiggy Texas Instruments Teladoc Health Walmart Zynga Ubisoft Mastercard Bloomberg Stripe Reddit Zomato Deloitte McKinsey & Company Google Microsoft Amazon Meta Apple Netflix Adobe