(JAVA)DP + Bitmasking  |  2ms
\nTimeComplexity : (m * 2 ^ m) * n\nSpace Complexity: m * (2 ^ m)\n\n```\nclass Solution {\n\n\tstatic int dp[][] = null;\n\tstatic int all_visited = 0;\n\tint 
\nTimeComplexity : (m * 2 ^ m) * n\nSpace Complexity: m * (2 ^ m)\n\n```\nclass Solution {\n\n\tstatic int dp[][] = null;\n\tstatic int all_visited = 0;\n\tint fun(int pos, int mask, int[][] students, int[][] mentors, int m, int n) {\n\n\n\t\tif (mask == all_visited) {\n\t\t\tdp[pos][mask] = 0;\n\t\t\treturn 0;\n\t\t}\n\n\t\tif (dp[pos][mask] != -1)\n\t\t\treturn dp[pos][mask];\n\n\n\t\tint max1 = 0;\n\t\tfor (int i = 0; i < m; i++) {   // try to find best matching between student and mentor\n\n\t\t\tif ((mask & (1 << i)) == 0) {\n\n\t\t\t\tint cal = 0;            // used for calculating how many answers matched in student and mentor\n\t\t\t\tfor (int k = 0; k < n; k++) {\n\t\t\t\t\tif (students[pos][k] == mentors[i][k])\n\t\t\t\t\t\tcal++;\n\t\t\t\t}\n\n\t\t\t\tmax1 = Math.max(max1, cal + fun(pos + 1, (mask | (1 << i)), students, mentors, m, n));\n\t\t\t}\n\t\t}\n\n\t\tdp[pos][mask] = max1;\n\t\treturn max1;\n\t}\n\n\n\tpublic int maxCompatibilitySum(int[][] students, int[][] mentors) {\n\n\n\t\tint m = students.length;\n\t\tint n = students[0].length;\n\t\tall_visited = (1 << m) - 1;       // to mark that all students all matched with all mentors\n\t\tdp  = new int[m + 1][1 << (m + 1)];\n\n\t\tfor (int i = 0; i < dp.length; i++) {\n\t\t\tfor (int j = 0; j < dp[0].length; j++)\n\t\t\t\tdp[i][j] = -1;            // for marked all as unvisited/uncalculated\n\t\t}\n\n\t\treturn fun(0, 0, students, mentors, m, n);\n\t}\n
[]