i was trying to solve job assignment problem
geeksforgeeks problem link
geeksforgeeks solution article
i started to solve job assingment problem using naive approach using recursion and got
TLE on geeksforgeeks
as i came to know using permutation and combination concept complexity for my recursion approach is O(n!)
but dont know how to calculate complexity using recursion tree or any other approach
below is my Code for naive approach
public class AssignmentProblemUsingRecursionNoMemoization {
static int jobs;
static int taskCost[][];
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int T = s.nextInt();
for (int i = 0; i < T; i++) {
jobs = s.nextInt();
//cost of each task by each machine.. rows==task , coulumn== machine
taskCost = new int[jobs][jobs];
for (int j = 0; j < jobs; j++) {
for (int k = 0; k < jobs; k++) {
taskCost[j][k] = s.nextInt();
}
}
System.out.println(calculate(0, 0, new int[jobs]));
}
}
public static int calculate(int taskNumberjoHogaab, int costTillNow, int arrJoTaskJoHogyeYaNhi_Btayega[]) {
if (taskNumberjoHogaab == jobs) {
return 0;
}
int min = 101;
for (int k = 0; k < arrJoTaskJoHogyeYaNhi_Btayega.length; k++) {
if (arrJoTaskJoHogyeYaNhi_Btayega[k] == 0) {
int newArr[] = copyArr(arrJoTaskJoHogyeYaNhi_Btayega);
newArr[k] = 1;
min = Math.min(taskCost[taskNumberjoHogaab][k] + calculate(taskNumberjoHogaab + 1, costTillNow + taskCost[taskNumberjoHogaab][k], newArr), min);
}
}
return min;
}
public static int[] copyArr(int arr[]) {
int newArray[] = new int[arr.length];
for (int p = 0; p < arr.length; p++) {
newArray[p] = arr[p];
}
return newArray;
}
}
to optimise my code i used recursion with memoization and submitted my code and got TLE again(shocked)
but i am not able to identify time complexity of my code… plz anyOne Help me to find out complexity of my below code and why it is not working and giving TLE
public class AssignmentProblemUsingRecursionMemoization {
static int jobs;
static int taskCost[][];
static HashMap<String, Integer> map;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int T = s.nextInt();
for (int i = 0; i < T; i++) {
jobs = s.nextInt();
//cost of each task by each machine.. rows==task , coulumn== machine
taskCost = new int[jobs][jobs];
map = new HashMap<String, Integer>();
for (int j = 0; j < jobs; j++) {
for (int k = 0; k < jobs; k++) {
taskCost[j][k] = s.nextInt();
}
}
System.out.println(calculate(0, 0, new int[jobs]));
}
}
public static int calculate(int taskNumberjoHogaab, int costTillNow, int arrJoTaskJoHogyeYaNhi_Btayega[]) {
if (taskNumberjoHogaab == jobs) {
return 0;
}
String arrStr = arrayToString(arrJoTaskJoHogyeYaNhi_Btayega);
if (map.get(arrStr) != null) {
return map.get(arrStr);
}
int min = 0;//101;
for (int k = 0; k < arrJoTaskJoHogyeYaNhi_Btayega.length; k++) {
if (arrJoTaskJoHogyeYaNhi_Btayega[k] == 0) {
int newArr[] = copyArr(arrJoTaskJoHogyeYaNhi_Btayega);
newArr[k] = 1;
int tempCost = taskCost[taskNumberjoHogaab][k] + calculate(taskNumberjoHogaab + 1, costTillNow + taskCost[taskNumberjoHogaab][k], newArr);
if (min == 0) {
min = tempCost;
} else {
min = Math.min(tempCost, min);
}
}
}
map.put(arrayToString(arrJoTaskJoHogyeYaNhi_Btayega), min);
return min;
}
public static int[] copyArr(int arr[]) {
int newArray[] = new int[arr.length];
for (int p = 0; p < arr.length; p++) {
newArray[p] = arr[p];
}
return newArray;
}
public static String arrayToString(int arr[]) {
String str = "";
for (int k = 0; k < arr.length; k++) {
str = str + arr[k];
}
return str;
}
}