Le migliori 150 intervisteDifficile

Sottoarray massimo

Guida dettagliata e implementazione Python per il problema 'Maximum Subarray'.

Dichiarazione del problema

Difficile

Dato un array intero nums, trova il sottoarray contiguo (contenente almeno un numero) che ha la somma più grande e restituisce la sua somma.

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

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

Esempi

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

[4,-1,2,1] has the largest sum = 6.

Example 2
Input
nums = [1]
Output
1
Explanation

[1] has the largest sum = 1.

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.