Programmazione competitivaMedio

Matrice lessicograficamente più grande

Guida dettagliata e implementazione Python per il problema dell'array lessicograficamente più grande.

Dichiarazione del problema

Medio

Scrivi una funzione lexicographically_largest(arr, k) che restituisca l'array lessicograficamente più grande che può essere ottenuto scambiando elementi adiacenti al massimo k volte. Gli elementi in arr sono unici.

Vincoli
  • 1 <= len(arr) <= 1000
  • 0 <= k <= 10^5
  • -10^9 <= arr[i] <= 10^9

Esempi

Example 1
Input
lexicographically_largest([1, 2, 3, 4, 5], 3)
Output
[4, 1, 2, 3, 5]
Explanation

To get the largest possible element 4 as close to the front as possible, we swap it forward, requiring 3 swaps: [1, 2, 3, 4, 5] -> [1, 2, 4, 3, 5] -> [1, 4, 2, 3, 5] -> [4, 1, 2, 3, 5].

Example 2
Input
lexicographically_largest([3, 5, 4, 1, 2], 1)
Output
[5, 3, 4, 1, 2]
Explanation

Swap 3 and 5 (1 swap) to get [5, 3, 4, 1, 2].

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