const solution = matrix => {
const length = matrix[0].length;
const hash = Array(length)
.fill(null)
.map((v, i) => i + 1)
.reduce((acc, key, _, origin) => {
const innerHash = {};
for (const key of origin) {
innerHash[key] = null;
}
acc[key] = innerHash;
return acc;
}, {});
console.log(0, '가능한 모든 경우의 수를 hash로 표현', hash);
matrix.forEach(row => {
const deleteList = [];
row.forEach(student => {
deleteList.push(student);
console.log(
`${student}번 학생의 멘티가 될 수 없는 학생 목록`,
deleteList,
);
deleteList.forEach(value => {
if (hash[student].hasOwnProperty(value)) delete hash[student][value];
});
console.log(
`${student}번 학생의 멘티가 될 수 없는 학생을 소거하고 난 뒤의 hash : `,
hash,
'\n',
);
});
});
let count = 0;
for (key in hash) for (a in hash[key]) count++;
return count;
};
const result = solution([
[3, 4, 1, 2],
[4, 3, 2, 1],
[3, 1, 4, 2],
]);
console.log(result);