Constructs a top-down dynamic programming solution for the rod-cutting problem via memoization. This function serves as a wrapper for ``_top_down_cut_rod_recursive`` Runtime: O(n^2) Arguments --------- * `n`: int, the length of the rod * `prices`: list, the prices
(n: int, prices: list)
| 54 | |
| 55 | |
| 56 | def top_down_cut_rod(n: int, prices: list): |
| 57 | """ |
| 58 | Constructs a top-down dynamic programming solution for the rod-cutting |
| 59 | problem via memoization. This function serves as a wrapper for |
| 60 | ``_top_down_cut_rod_recursive`` |
| 61 | |
| 62 | Runtime: O(n^2) |
| 63 | |
| 64 | Arguments |
| 65 | --------- |
| 66 | |
| 67 | * `n`: int, the length of the rod |
| 68 | * `prices`: list, the prices for each piece of rod. ``p[i-i]`` is the |
| 69 | price for a rod of length ``i`` |
| 70 | |
| 71 | .. note:: |
| 72 | For convenience and because Python's lists using ``0``-indexing, ``length(max_rev) |
| 73 | = n + 1``, to accommodate for the revenue obtainable from a rod of length ``0``. |
| 74 | |
| 75 | Returns |
| 76 | ------- |
| 77 | |
| 78 | The maximum revenue obtainable for a rod of length `n` given the list of prices |
| 79 | for each piece. |
| 80 | |
| 81 | Examples |
| 82 | -------- |
| 83 | |
| 84 | >>> top_down_cut_rod(4, [1, 5, 8, 9]) |
| 85 | 10 |
| 86 | >>> top_down_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]) |
| 87 | 30 |
| 88 | """ |
| 89 | _enforce_args(n, prices) |
| 90 | max_rev = [float("-inf") for _ in range(n + 1)] |
| 91 | return _top_down_cut_rod_recursive(n, prices, max_rev) |
| 92 | |
| 93 | |
| 94 | def _top_down_cut_rod_recursive(n: int, prices: list, max_rev: list): |
no test coverage detected