150 principais entrevistasFácil

Mesclar K listas classificadas

Guia detalhado e implementação de Python para o problema 'Merge K Sorted Lists'.

Declaração do problema

Fácil

Você recebe uma matriz de k listas vinculadas, cada lista vinculada é classificada em ordem crescente.

Mesclar todas as listas vinculadas em uma lista vinculada classificada e retorná-la.

As listas vinculadas são representadas como uma lista de listas Python. Implemente uma função mergeKLists(lists: list) -> list que retorna a lista classificada mesclada.

Restrições
  • 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

Exemplos

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?
Considere usar estruturas de dados específicas de listas vinculadas, 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.