Programmazione competitivaFacile

Taglio dell'asta

Guida dettagliata e implementazione Python per il problema 'Rod Cutting'.

Dichiarazione del problema

Facile

Scrivi una funzione cut_rod(price, n) che prenda un elenco di prezzi price dove price[i] è il prezzo di un'asta di lunghezza i+1 e una lunghezza totale n. Restituisce il valore massimo ottenibile tagliando l'asta di lunghezza n e vendendo i pezzi.

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

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche della programmazione dinamica 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.