150 principais entrevistasFácil

Intervalos não sobrepostos

Guia detalhado e implementação de Python para o problema de 'Intervalos Não Sobrepostos'.

Declaração do problema

Fácil

Dada uma matriz de intervalos onde intervals[i] = [starti, endi], retorne o número mínimo de intervalos que você precisa remover para fazer com que o restante dos intervalos não se sobreponham.

Escreva uma função eraseOverlapIntervals(intervals: List[List[int]]) -> int.

Restrições
  • 1 <= len(intervals) <= 10^5
  • intervals[i].length == 2
  • -5 * 10^4 <= starti < endi <= 5 * 10^4

Exemplos

Example 1
Input
intervals = [[1,2],[2,3],[3,4],[1,3]]
Output
1
Explanation

[1,3] can be removed and the rest are non-overlapping.

Example 2
Input
intervals = [[1,2],[1,2],[1,2]]
Output
2
Explanation

Remove two [1,2] to make the rest non-overlapping.

Example 3
Input
intervals = [[1,2],[2,3]]
Output
0
Explanation

Already non-overlapping.

Need a Hint?
Considere usar estruturas de dados específicas de intervalos, como conjuntos ou heaps.
Edge Cases to Watch
  • Estruturas de entrada vazias
  • Entradas de elemento único
  • Grandes limites numéricos

Pronto para resolver?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Abrir no Editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.