Given a positive integer N, find the maximum number of segments of lengths a, b and c that can be formed from N .
Input : N = 7, a = 5, b, = 2, c = 5 Output : 2 N can be divided into 2 segments of lengths 2 and 5. For the second example, Input : N = 17, a = 2, b = 1, c = 3 Output : 17 N can be divided into 17 segments of 1 or 8 segments of 2 and 1 segment of 1. But 17 segments of 1 is greater than 9 segments of 2 and 1.
Approach : The approach used is Dynamic Programming. The base dp0 will be 0 as initially it has no segments. After that, iterate from 1 to n, and for each of the 3 states i.e, dpi+a, dpi+b and dpi+c, store the maximum value obtained by either using or not using the a, b or c segment.
The 3 states to deal with are :
Below is the implementation of above idea :
Time complexity : O(n)
- Maximum length of segments of 0's and 1's
- Find the maximum number of composite summands of a number
- LCS formed by consecutive segments of at least length K
- Divide the array in K segments such that the sum of minimums is maximized
- Partition the array in K segments such that bitwise AND of individual segment sum is maximized
- Maximum sum of a path in a Right Number Triangle
- How to print maximum number of A's using given four keys
- Possible cuts of a number such that maximum parts are divisible by 3
- Recursively break a number in 3 parts to get maximum sum
- Paths with maximum number of 'a' from (1, 1) to (X, Y) vertically or horizontally
- Maximum number of given operations to remove the entire string
- Maximum number of trailing zeros in the product of the subsets of size k
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Super Ugly Number (Number whose prime factors are in given set)
- Number of ways to represent a number as sum of k fibonacci numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.