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)
- Size of The Subarray With Maximum Sum
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Maximum value with the choice of either dividing or considering as it is
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Maximum path sum for each position with jumps under divisibility condition
- Maximum sum of pairs with specific difference
- Recursively break a number in 3 parts to get maximum sum
- Partition a set into two subsets such that the difference of subset sums is minimum
- Find the longest path in a matrix with given constraints
- Maximum Product Cutting | DP-36
- Cutting a Rod | DP-13
- Coin Change | DP-7
- Longest Common Subsequence | DP-4
- Maximum size square sub-matrix with all 1s
- Largest Sum Contiguous Subarray
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.