멘토링

WooBuntu·2021년 2월 18일
0

JS 90제

목록 보기
13/33
  • 강의 듣기 전 내 풀이
const solution = matrix => {
  const length = matrix[0].length;

  // 1. 이중 해쉬 만들기
  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);

  // 2. 이중 해쉬에서 불가능한 경우의 수 소거
  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);

0개의 댓글

관련 채용 정보