
Suppose, you have 7 apples and multiple boxes of capacities 2, 3 and 5.
You can keep these 7 apples in maximum 3 boxes i.e. two boxes of capacity 2 and one box of capacity 3.
The first line of input contains an integer 'T' denoting the number of test cases.
The first line of each test case contains a single integer 'N' denoting the number of apples.
The second line of each test case contains three space-separated integers 'A', 'B', and 'C' denoting the capacity of three types of boxes.
For each test case, return the maximum number of boxes in which you can distribute these 'N' apples.
Print the output of each test case in a separate line.
1. If it is impossible to distribute the apples into the boxes, simply return 0.
2. You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10 ^ 5
1 <= A, B, C <= 10 ^ 4
Time limit: 1 sec
To optimize the overlapping sub-problem calculation, we will use memoization by storing answers for each recursive state. We can see that we were solving the same subproblems multiple times. Thus, this problem was exhibiting overlapping subproblems.
This means, in this approach, we need to eliminate the need for solving the same subproblems again and again. Thus, the idea is to use Memoization.
The steps are as follows:
Int maximumBoxesHelper('N', ‘A’, ‘B’, ‘C’):
Note: If ‘ANS’ is negative means no distribution is possible, so return ‘ANS’ = 0.
Initially, we were breaking the large problem into small problems but now, let us look at it differently. The idea is to solve the small problem first and then reach the final answer. Thus we will be using a bottom-up approach now.
The steps are as follows:
The detailed algorithm to fill the ‘DP’ array will be as follows:
Loop : For i = 1 to ‘N’:
Let us understand the algorithm with an example:
Consider ‘N’ = 5, ‘A’ = 2, ‘B’ = 3 and ‘C’ = 5