Applied AI Engineer (LLM) coding learning path Level 2 This level · 24 of 31

Non-overlapping Intervals

Medium Greedy Sorting

Problem

Return the minimum number of intervals you must remove so that the rest are non-overlapping. Two intervals that only touch at an endpoint do not overlap.

Original: LeetCode 435: Non-overlapping Intervals.

Examples

Example 1

Input`intervals = [[1, 2], [2, 3], [3, 4], [1, 3]]`
Output`1`

Example 2

Input`intervals = [[1, 2], [1, 2], [1, 2]]`
Output`2`

Example 3

Input`intervals = [[1, 2], [2, 3]]`
Output`0`

Constraints

  • 1 <= intervals.length <= 10^5
  • intervals[i].length == 2
  • -5 * 10^4 <= starti < endi <= 5 * 10^4

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (1)
  1. LeetCode 435: Non-overlapping Intervalsleetcode.com
Previous
Next