Ninja is planning to organize an exhibition in which ‘2N’ peoples are coming. The hall’s capacity in which the exhibition is going to take place is only ‘N’. So, ninja thinks of organizing two exhibitions in 2 different cities, ‘CITY_A’ and ‘CITY_B’, each of capacity ‘N’. All ‘2N’ people will come to the exhibition but in the group of ‘N’ in two different cities. Ninja wants your help in organizing this event.
You are given an array ‘COST’ where “COST[i] = [COST_TO_Ai, COST_TO_Bi]”, in which ‘COST_TO_Ai’ is the cost of the ith person to fly to the city A, and ‘COST_TO_Bi’ is the cost of the ith person to fly to the city B. Your task is to find the minimum cost of organizing the exhibition in which every person flies to a city such that exactly ‘N’ people arrive in each city.
Note:
The intersection of the set of the persons who go to ‘CITY_A’ to those going to ‘CITY_B’ is to be disjoint set, whereas the union must be the ‘2N’.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the 'T' test cases follow.
The first line of each test case contains a positive integer, ‘N’, such that ‘2N’ is the number of people coming to the exhibition.
The next ‘2N’ lines of each test case contain two integers, ‘COST_TO_Ai’, and ‘COST_TO_Bi’, as described in the problem statement.
Note:
The ‘COST’ will be passed to the function as an array of arrays. Each array will contain two integers, ‘COST_TO_Ai’, and ‘COST_TO_Bi’ in that order.
Output Format:
For each test case, return the minimum cost of organizing the exhibition, as described in the problem statement.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 50
COST.length == 2*N
1 <= CostToAi, CostToBi <= 10^4
Time Limit: 1sec
1
2
5 100
50 10
10 200
100 20
45
The first person will go to City ‘A’ at the cost of 5.
The second person will go to City ‘B’ at the cost of 10.
The third person will go to City ‘A’ at the cost of 10.
The last person will go to City ‘B’ at the cost of 20.
So, the total minimum cost will be = 5 + 10 + 10 + 20 = 45. There is no other way to get the cost less than 45.
1
1
200 200
100 200
300
The first person will go to City ‘B’ at the cost of 200.
The second person will go to City ‘A’ at the cost of 100.
So, the total minimum cost will be = 200 + 100 = 300. There is no other way to get the cost less than 300.
Think of a recursive solution with memoization.
The idea here is to use the recursion. For every ‘ith’ person, we have two options available. The first one is to fly to ‘City A’ for the cost of ‘COST_TO_Ai’, and the second is to fly to ‘City B’ for the cost of ‘COST_TO_Bi’. If at any point we have sent ‘N’ persons to one city, then we have to send the remaining peoples to the other city only. We have to always return the minimum in every function call. To avoid the function to again compute the result for the same function call, we can save the previously called function’s output that is nothing but the memoization.
O(N^2), where ‘N’ is the number of peoples in each city.
There are 2 options available for every person to go to either City ‘A’ or City ‘B’. From this, you can say that it is a 2^N solution, but if you look carefully, we are using the memoization technique, which will reduce the time complexity to O(N^2).
O(N^3), where ‘N’ is the number of peoples in each city.
As we are using the three states dynamic programming. So, the space complexity will be O(N^3).