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)
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Minimum number of jumps to reach end
- Maximum sum such that no two elements are adjacent
- Maximum size square sub-matrix with all 1s
- Total number of possible Binary Search Trees and Binary Trees with n keys
- Maximum Sum Increasing Subsequence | DP-14
- Maximum Length Chain of Pairs | DP-20
- Efficient program to print all prime factors of a given number
- Maximum sum rectangle in a 2D matrix | DP-27
- Maximum Subarray Sum using Divide and Conquer algorithm
- Maximum Product Cutting | DP-36
- Program for nth Catalan Number
- Count number of binary strings without consecutive 1's
- Count number of ways to reach a given score in a game
- How to print maximum number of A's using given four keys
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.