Given a rod of length L, the task is to cut the rod in such a way that the total number of segments of length p, q and r is maximized. The segments can only be of length p, q, and r.
Input: l = 11, p = 2, q = 3, r = 5
Segments of 2, 2, 2, 2 and 3
Input: l = 7, p = 2, q = 5, r = 5
Segments of 2 and 5
Approach: Dynamic Programming is used to solve this problem. Initialize dp array to 0. Iterate till the length of the rod. For every i, a cut of p, q and r if possible is done. Initialize ans[i+p] = max( ans[i+p], 1 + ans[i]), ans[i+q] = max(ans[i+q], 1 + ans[i]) and ans[i+r] = max(ans[i+r], 1 + ans[i]) for all the possible cuts. ans[i] will be 0 if a cut at i-th index is not possible. ans[l] will give the maximum number of cuts possible.
Below is the implementation of the above approach:
Time Complexity: O(N)
- Number of segments where all elements are greater than X
- Number of horizontal or vertical line segments to connect 3 points
- Minimum number of cuts required to make circle segments equal sized
- Maximize the number of subarrays with XOR as zero
- Maximize the product of four factors of a Number
- Rearrange the string to maximize the number of palindromic substrings
- Number of balanced bracket subsequence of length 2 and 4
- Convert a number of length N such that it contains any one digit at least 'K' times
- Number of different cyclic paths of length N in a tetrahedron
- Find consecutive 1s of length >= n in binary representation of a number
- Check if it is possible to reach a number by making jumps of two given length
- Number of squares of side length required to cover an N*M rectangle
- Divide N segments into two non-empty groups such that given condition is satisfied
- Maximize the value of x + y + z such that ax + by + cz = n
- Maximize the bitwise OR of an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.