Blind 75
65 / 75Free practice

Non-overlapping Intervals

Medium · Intervals

Jump to code

Remove the fewest intervals so the remaining intervals never overlap. Here an interval ending at time t does not conflict with one starting at t. Each interval has start < end.

Examples

Example 1

intervals = [[1,2],[2,3],[3,4],[1,3]]

Output: 1

Example 2

intervals = [[1,2],[1,2],[1,2]]

Output: 2

Constraints

  • 1 <= intervals.length <= 1000
  • intervals[i].length == 2
  • -50000 <= starti < endi <= 50000
Need a hint?

Keeping the interval that finishes earliest leaves the most room.

Real interval interview questions

Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    [
      1,
      2
    ],
    [
      2,
      3
    ],
    [
      3,
      4
    ],
    [
      1,
      3
    ]
  ]
]
Expected
1
Case 2
Input
[
  [
    [
      1,
      2
    ],
    [
      1,
      2
    ],
    [
      1,
      2
    ]
  ]
]
Expected
2
Case 3
Input
[
  [
    [
      1,
      2
    ],
    [
      2,
      3
    ]
  ]
]
Expected
0
Case 4
Input
[
  [
    [
      1,
      5
    ],
    [
      2,
      3
    ]
  ]
]
Expected
1
Case 5
Input
[
  [
    [
      1,
      2
    ],
    [
      1,
      3
    ],
    [
      2,
      4
    ]
  ]
]
Expected
1

Drafts are saved per language in this browser when storage is available.