![]() |
VOOZH | about |
Given a rod of length n, the task is to cut the rod in such a way that the total number of segments of length x, y, and z is maximized. The segments can only be of length x, y, and z.
Note: If no segment can be cut then return 0.
Examples:
Input: n = 4, x = 2, y = 1, z = 1
Output: 4
Explanation: Total length is 4, and the cut lengths are 2, 1 and 1. We can make maximum 4 segments each of length 1.Input: n = 5, x = 5, y = 3, z = 2
Output: 2
Explanation: Here total length is 5, and the cut lengths are 5, 3 and 2. We can make two segments of lengths 3 and 2.Input: n = 7, x = 8, y = 9, z = 10
Output: 0
Explanation: Here the total length is 7, and the cut lengths are 8, 9, and 10. We cannot cut the segment into lengths that fully utilize the segment, so the output is 0.
Table of Content
For the recursive approach, the function explores all possible ways to cut the rod into segments of given lengths. At each step, it considers three options:
The recurrence relation for the problem is as follows:
if n < 0
maxCutHelper(n, x, y, z) = -1,else
maxCutHelper(n, x, y, z) = 1 + max(maxCutHelper(n - x, x, y, z), maxCutHelper(n - y, x, y, z), maxCutHelper(n - z, x, y, z))Base Cases:
- If the rod length is zero (n = 0), return 0 (no more cuts can be made).
- If the rod length becomes negative (n < 0), return -1 (invalid result).
5
The recursive approach is optimized using memoization, storing previously computed results in a memo array to avoid redundant computations. The two key properties of dynamic programming are utilized:
1. Optimal Substructure: The solution for maximizing cuts can be derived from smaller subproblems:
- If the rod length n is zero, the number of cuts is zero.
- If the rod length becomes negative, the result is invalid (-1).
- Otherwise, for each segment length (x, y, z), the solution is the maximum result of reducing the rod by that segment length and recursively solving the subproblem.
2. Overlapping Subproblems: The function repeatedly computes results for the same rod length (n) with the same segment lengths. Using a memoization array memo, where memo[n] stores the result for rod length n, avoids redundant calculations.
5
The approach is similar to the previous one, but instead of breaking down the problem recursively, we iteratively build up the solution by calculating in a bottom-up manner. So, we will create a 1D array dp of size (n + 1) of type integer. The state dp[i] will store the maximum number of cuts possible for a rod of length i.
The dynamic programming relation is as follows:
- If the current rod length i is less than the segment length x: dp[i] = dp[i]
- Otherwise, we update dp[i] as: dp[i] = max(dp[i], dp[i - k] + 1), where k can be x, y, or z.
This means that if the current rod length is smaller than the segment length, no cut is possible for this length, and we keep the previous value. Otherwise, we check if adding a segment of length k results in a greater number of cuts and update the value of dp[i] accordingly. Finally, if no valid cuts are possible for the rod of length n, we return 0. Otherwise, the value at dp[n] will give the maximum number of cuts.
5