Rod Cutting
Implement rodCutting
A rod of a certain length can be sold whole or cut into smaller pieces, each length having its own fixed selling price. Given the price for every possible piece length from 1 up to the rod's full length, decide how to cut the rod — into any number of pieces of any lengths, each used as often as needed — to maximize the total revenue from selling all the pieces.
Every rod length boils down to a choice of where to make the first cut: sell the piece cut off at that length for its listed price, then face the exact same decision on whatever length remains. Trying every possible first-cut length and combining its price with the best revenue already known for the leftover length finds the best total for that starting length. Because shorter lengths are solved before longer ones ever need them, building the answer up from length 0 to the full rod length turns an exponential search into a simple sweep.
Example 1:
Input: prices = [3,6,4,9,2]
Output: 15
Example 2:
Input: prices = [5,8]
Output: 10
Example 3:
Input: prices = [6,4,10,3,12]
Output: 30
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ prices.length ≤ 20 - ●
0 ≤ prices[i] ≤ 1000
prices =
[3, 6, 4, 9, 2]