Python BasesDifficile

Trouver le sous-tableau de produits maximum

Guide détaillé et implémentation de Python pour le problème « Trouver le sous-tableau de produits maximum ».

Énoncé du problème

Difficile

Écrivez une fonction max_product_subarray(arr) qui prend une liste d'entiers arr et renvoie le produit maximum de tout sous-tableau contigu. Un sous-tableau doit contenir au moins un élément.

Contraintes
  • 1 <= len(arr) <= 10^4
  • -10 <= arr[i] <= 10

Exemples

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

The subarray [2, 3] has the maximum product: 2 * 3 = 6.

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

The subarray [0] gives the maximum product of 0, since all other products are negative or zero.

Example 3
Input
arr = [-2, -3, 4]
Output
24
Explanation

The entire array: (-2) * (-3) * 4 = 24? Wait, that's the subarray [-2,-3,4] = 24. Yes, two negatives multiply to positive.

Need a Hint?
Pensez à utiliser des structures de données spécifiques aux tableaux, comme des ensembles ou des tas.
Edge Cases to Watch
  • Structures d'entrée vides
  • Entrées à élément unique
  • Grandes limites numériques

Prêt à résoudre ?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Ouvrir dans l'éditeur
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

Ressources Python recommandées

Développez vos connaissances avec des didacticiels interactifs, des aide-mémoire et des comparaisons de codes associés.