Le migliori 150 intervisteDifficile

Sottoarray prodotto massimo

Guida dettagliata e implementazione Python per il problema "Maximum Product Subarray".

Dichiarazione del problema

Difficile

Dato un array intero nums, trova un sottoarray contiguo non vuoto all'interno dell'array che ha il prodotto più grande e restituisce il prodotto.

I casi di test vengono generati in modo che la risposta rientri in un numero intero a 32 bit.

Scrivi una funzione maxProduct(nums: List[int]) -> int.

Vincoli
  • 1 <= len(nums) <= 2 * 10^4
  • -10 <= nums[i] <= 10
  • The product of any prefix or suffix of nums fits in a 32-bit integer

Esempi

Example 1
Input
nums = [2,3,-2,4]
Output
6
Explanation

[2,3] has the largest product 6.

Example 2
Input
nums = [-2,0,-1]
Output
0
Explanation

The result cannot be larger than 0 because of the negative numbers separated by 0.

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