Le migliori 150 intervisteFacile

Unisci elenchi ordinati K

Guida dettagliata e implementazione Python per il problema "Unisci elenchi ordinati K".

Dichiarazione del problema

Facile

Ti viene fornita una serie di k elenchi di elenchi collegati, ciascun elenco collegato è ordinato in ordine crescente.

Unisci tutti gli elenchi collegati in un elenco collegato ordinato e restituiscilo.

Gli elenchi collegati sono rappresentati come un elenco di elenchi Python. Implementa una funzione mergeKLists(lists: list) -> list che restituisce l'elenco ordinato unito.

Vincoli
  • k == lists.length
  • 0 <= k <= 10000
  • 0 <= lists[i].length <= 500
  • -10000 <= lists[i][j] <= 10000
  • lists[i] is sorted in ascending order
  • The sum of lists[i].length will not exceed 10000

Esempi

Example 1
Input
[[1,4,5],[1,3,4],[2,6]]
Output
[1,1,2,3,4,4,5,6]
Explanation

Merging [1,4,5], [1,3,4], and [2,6] gives [1,1,2,3,4,4,5,6].

Example 2
Input
[]
Output
[]
Explanation

No lists to merge, so the result is empty.

Example 3
Input
[[]]
Output
[]
Explanation

One empty list results in an empty merged list.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche dell'elenco collegato 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.