

Let say N = 2 and K = 3 and costs = [ [1,5,3] , [2,9,4] ]
In this case,
Ninja can paint house 0 into color 0, paint house 1 into color 2. Minimum cost: 1 + 4 = 5;
Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost: 3 + 2 = 5 .
Assume 0 based indexing
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.
The first line of each test case contains two integers ‘N’ and ‘K’ representing the number of houses and the number of colors available respectively.
The next ‘N’ line contains ‘K’ integers denoting the cost of painting i-th house with j-th color where 0 <= i < n & 0 <= j < k.
For each test case print the minimum cost of painting the houses.
Print the output of each test case in a separate line.
You do not need to print anything or take input. This already has been taken care of. Just implement the function.
1 <= T <= 5
1 <= n, k <= 10^4
1 <= costs[i][j] <= 10^3
Time limit: 1 sec
For choosing a color for each house we have to keep two things in mind:
We can solve this by a simple recursion in the following steps;
Algorithm
We can solve this problem using a 2D dp array. For every house check cost of coloring it with k colors, then search the minimum cost (if not use certain color).
Algorithm
Algorithm