Programowanie konkurencyjneŁatwe

Mnożenie łańcucha macierzy

Szczegółowy przewodnik i implementacja Python dla problemu „Mnożenie łańcucha macierzy”.

Oświadczenie o problemie

Łatwe

Napisz funkcję matrix_chain_order(p), która pobiera tablicę p reprezentującą wymiary łańcucha macierzy tak, że macierz i ma wymiar p[i-1] x p[i]. Znajdź minimalną liczbę mnożeń przez skalar potrzebnych do pomnożenia łańcucha macierzy.

Ograniczenia
  • 2 <= len(p) <= 100
  • 1 <= p[i] <= 500

Przykłady

Example 1
Input
matrix_chain_order([40, 20, 30, 10, 30])
Output
26000
Explanation

There are 4 matrices of dimensions 40x20, 20x30, 30x10, and 10x30. The minimum operations are obtained by multiplying them in the order ((A(BC))D).

Example 2
Input
matrix_chain_order([10, 20, 30, 40, 30])
Output
30000
Explanation

The minimum number of scalar multiplications is 30000.

Need a Hint?
Rozważ użycie struktur danych specyficznych dla programowania dynamicznego, 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.