Python PodstawyCiężko

Znajdź maksymalną podtablicę produktów

Szczegółowy przewodnik i implementacja Python dla problemu „Znajdź maksymalną podtablicę produktu”.

Oświadczenie o problemie

Ciężko

Napisz funkcję max_product_subarray(arr), która pobiera listę liczb całkowitych arr i zwraca maksymalny iloczyn dowolnej ciągłej podtablicy. Podtablica musi zawierać co najmniej jeden element.

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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla tablic, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.