Programowanie konkurencyjneŁatwe

Cięcie prętów

Szczegółowy przewodnik i implementacja Python dla problemu „Cięcie pręta”.

Oświadczenie o problemie

Łatwe

Napisz funkcję cut_rod(price, n), która pobiera listę cen price, gdzie price[i] jest ceną pręta o długości i+1 i długości całkowitej n. Zwróć maksymalną wartość możliwą do uzyskania poprzez pocięcie pręta o długości n i sprzedaż kawałków.

Ograniczenia
  • 1 <= n <= 1000
  • len(price) >= n
  • 1 <= price[i] <= 10000

Przykłady

Example 1
Input
cut_rod([1, 5, 8, 9, 10, 17, 17, 20], 8)
Output
22
Explanation

Cut the rod of length 8 into two pieces of length 2 and 6. The total value is 5 + 17 = 22.

Example 2
Input
cut_rod([3, 5, 8, 9, 10, 17, 17, 20], 8)
Output
24
Explanation

Cut the rod of length 8 into eight pieces of length 1. The total value is 8 * 3 = 24.

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.