Le migliori 150 intervisteFacile

Unisci intervalli

Guida dettagliata e implementazione Python per il problema "Unisci intervalli".

Dichiarazione del problema

Facile

Dato un array di intervalli in cui intervalli[i] = [starti, endi], unisci tutti gli intervalli sovrapposti e restituisce un array degli intervalli non sovrapposti che coprono tutti gli intervalli nell'input.

Scrivi una funzione merge(intervals: List[List[int]]) -> List[List[int]].

Vincoli
  • 1 <= len(intervals) <= 10^4
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 10^4

Esempi

Example 1
Input
intervals = [[1,3],[2,6],[8,10],[15,18]]
Output
[[1,6],[8,10],[15,18]]
Explanation

Intervals [1,3] and [2,6] overlap, merge to [1,6].

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

[1,4] and [4,5] overlap.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche per gli intervalli come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.