Input: ‘N’ = 4 , 'SALARY' = [[8000,10000],[16000,25000],[10000,11000],[16000,20000]]
Output: 53000
Explanation:
Array elements represent the SALARY expectations of candidates sorted in decreasing order of problem-solving skills.
The first and second candidates can be put in a team, with the senior role for the first, so 10000+16000 = 26000.
The third and fourth candidates can be put in a team, with the senior role for the third, so 11000+16000 = 27000.
Thus, The Cost to Company = 26000 + 27000 = 53000.
The first line of the input contains an integer denoting the number of test cases.
The first line of each test case contains an even integer, 'N’, denoting the number of candidates.
The next ‘N’ lines of each test case contain 2 space-separated integers denoting SALARY expectations for the junior role and senior role in order.
Return an integer denoting the minimum cost to the company.
1 <= T <= 10
2 <= N <= 10000
1 <= SALARY[i][0] < SALARY[i][1] <= 100000
Time Limit: 1 sec
The brute force solution for the problem lies in generating all the permutations for pairing the candidates and returning the minimum cost to the company.
We can store the salary expectations along with the problem-solving skills for each candidate in ‘sal’ array with ‘N’ rows for every candidate and 3 columns for problem-setting skills, and expectation for junior and senior roles. We will then generate all the permutations of the ‘sal’ and for each permutation, we will pair them up by looking at the problem-solving skills and update the minimum to the answer.
The first candidate should be assigned to a senior role. Now from the second candidate, we can minimize the cost to the company by assigning him the senior or junior role. The problem can be solved using dynamic programming.
States: 2 State DP - role(0 or 1) and index.
DP[ role ][ index ] = Minimum cost to the company if the current index candidate is assigned the role 0 or 1 (senior or junior).
Transition: Let ‘idx’ be the current index in range 1 to ’N’/2, ‘idxBefore’ be the indexes in range 1 to idx.
DP[role][idxBefore]=min(DP[role][idxBefore-1]+SALARY[idx+idxBefore-1][0],DP[!role][idxBefore]+SALARY[idx+idxBefore-1][1])
For every value of idx in the range [1,’N’/2].
The steps are as follows :
3. Return DP[!role][N/2].