There are 'N' number of subjects and the ith subject contains subject[i] number of problems. Each problem takes 1 unit of time to be solved. Also, you have 'K' friends, and you want to assign the subjects to each friend such that each subject is assigned to exactly one friend. Also, the assignment of subjects should be contiguous. Your task is to calculate the maximum number of problems allocated to a friend is minimum. See example for more understanding.
For Example:
If N = 4, K = 2 and subjects = {50,100,300,400}
Assignment of problems can be done in the following ways among the two friends.
{} and {50,100,300,400}. Time required = max(0, 50+100+300+400) = max(0, 850) = 850
{50} and {100,300,400}. Time required = max(50, 100+300+400) = max(50, 800) = 800
{50, 100} and {300,400}. Time required = max(50+100, 300+400) = max(150, 700) = 700
{50,100,300} and {400}. Time required = max(50+100+300, 400) = max(450, 400) = 400
{50,100,300, 400} and {}. Time required = max(50+100+300+400, 0) = max(850, 0) = 850
So, out of all the above following ways, 400 is the lowest possible time.
Input format:
The first line of input contains a single integer 'T', representing the number of test cases or queries to be run.
Then the 'T' test cases follow.
The first line of each test case contains a single space-separated two integers 'N' and 'K' representing the total subjects and friends.
The second line of each test case contains 'N' space-separated integers representing the problems of the array “subjects”.
Output format:
For each test case, print the minimum possible time to solve all the problems.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N, K <= 10^4
1 <= subjects[i] <= 10^9
Time limit: 1 sec
We want to assign N number of subjects among K friends. So consider this as dividing the array into K partitions, where each partition denotes the subjects assigned to one of the friends. A...read more
In the previous approach, the algorithm recursively calculates and compares every possible partition at every possible element, which has many overlapping subproblems. i.e. a recursive function computes the same sub-problems again and again. So we can use memoization to overcome the overlapping subproblems. To reiterate, memoization is when we store the results of all previously solved subproblems and return the minimum time required to solve the problems, from the dp[i][j] if we encounter the problem that has already been solved.
Since there are two states those are changing in the recursive function, i.e. N and K, so we use the 2-dimensional array dp[][] to store all the subproblems, where dp[i][j] will store the result from 0 to i th friend and from 0 to j th subject.
Another optimization that can be done is that, like in the previous approach, instead of calculating the sum of the array from subjects[i] to subjects[N-1] again and again, we can create a prefixSum array to store the prefix sum of the array subjects and get the sum from subjects[i] to subjects[N-1] in constant time.
Steps:
- Create a prefixSum array of size N.
- The steps to calculate the prefix sum:
- Initialize all the elements of this array to 0.
- Make the first element of the prefixSum array equal to the first element of subjects.
- Then run a loop from i = 1 to N and do:
- prefixSum[i] = prefixSum[i-1] + subjects[i].
- Now, create a dp array of size (K+1 * N+1). Initialize the values in the dp array to -1.
- Implement, and call the function minimumRequiredTimeUtil(subjects, N, K, dp, prefixSum).
minimumRequiredTimeUtil(subjects, N, K, dp, prefixSum):
- If the value of K is 1, then we simply return prefixSum[N-1].
- If the value of N is 1, then we return subjects[0].
- If the value in the dp table is not -1, then we simply return that value.
- Else, initialize an ans variable to 10^18, and run a loop from i = 1 to N and do:
- ans = min(ans, max(minimumRequiredTimeUtil(subjects, i, K-1, dp, prefixSum), prefixSum[n-1] - prefixSum[i-1]))
- Finally, assign the ans to dp[K][N] and return the ans.
O(K*N), where N is the number of subjects and K is the number of friends.
In the worst case, extra space is required to create the dp array of size K*N i.e O(K*N) and also for the recursion stack. Hence, the overall complexity will be O(K*N).
Time Complexity: O(n^3)Explanation:O(K*N^2), where N is the number of subjects and K is the number of friends.
In the worst case, for each K partition, we iterate the whole array “subjects” once and in each iteration we put the divider at each index from 0 to the current index of “subjects”. Hence, the time complexity becomes O(K*N^2).
The idea is to use a bottom-up dynamic programming approach instead of a memoization approach. Here also, for each partition we have the choices to put the divider between the i th and i+1...read more
The idea here is to perform the binary search. We know that the binary search can be performed if the following conditions hold.
- The target value should lie in a particular range.
- The searc...read more
Top Google Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Top HR questions asked in Google Software Developer Intern
Reviews
Interviews
Salaries
Users/Month