Le migliori 150 intervisteDifficile

Finestra scorrevole massima

Guida dettagliata e implementazione Python per il problema "Finestra scorrevole massima".

Dichiarazione del problema

Difficile

Ti viene dato un array di numeri interi nums e un intero k che rappresenta la dimensione di una finestra scorrevole che si sposta dall'estrema sinistra dell'array all'estrema destra. Puoi vedere solo i numeri k nella finestra. Ogni volta che la finestra scorrevole si sposta verso destra di una posizione.

Restituisce il valore massimo in ciascuna finestra scorrevole.

Scrivi una funzione maxSlidingWindow(nums: List[int], k: int) -> List[int].

Vincoli
  • 1 <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= len(nums)

Esempi

Example 1
Input
nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
Output
[3, 3, 5, 5, 6, 7]
Explanation

Window [1,3,-1] max=3, [3,-1,-3] max=3, [-1,-3,5] max=5, [-3,5,3] max=5, [5,3,6] max=6, [3,6,7] max=7.

Example 2
Input
nums = [1], k = 1
Output
[1]
Explanation

Single element window, max is 1.

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