MediumArrayGreedySortingIntervals

Non-overlapping Intervals

LeetCode
1 approach, code in all languages

Given a set of intervals, figure out the smallest number of them you would need to delete so that the ones left behind never overlap. Intervals that only share an endpoint are considered compatible and do not need to be removed.

Return that minimum deletion count. This is equivalent to keeping the largest possible set of mutually compatible intervals and removing everything else.

Example 1

Input: intervals = [[1,2],[2,4],[1,4]]

Output: 1

Dropping [1,4] leaves [1,2] and [2,4], which only touch at 2 and so do not overlap.

Example 2

Input: intervals = [[1,3],[1,3],[1,3]]

Output: 2

All three cover the same span, so two of the duplicates must go.

Constraints

  • 1 ≤ intervals.length ≤ 10^5
  • intervals[i].length == 2
  • -5 * 10^4 ≤ start < end ≤ 5 * 10^4
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now