[코테] 신고 결과 받기

이동창·2022년 4월 11일
0

코딩테스트

목록 보기
1/5

문제발생

function solution(id_list, report, k) {
    const reportById = 
          new Array(id_list.length).fill(new Array(id_list.length).fill(0))
    
    console.log(reportById)
    
    for(let i in report){
        const str = report[i].split(' ')
        const a = id_list.indexOf(str[0])
        const b = id_list.indexOf(str[1])
        
        if(reportById[a][b] === 0){
            reportById[a][b]++
        }
        
        console.log(reportById)
    }
    
    console.log(reportById)
}

Input

["muzi", "frodo", "apeach", "neo"], ["muzi frodo", "apeach frodo", "frodo neo", "muzi neo", "apeach muzi"], 2

Output

[ [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ] ]
[ [ 0, 1, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 1, 0, 0 ] ]
[ [ 0, 1, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 1, 0, 0 ] ]
[ [ 0, 1, 0, 1 ], [ 0, 1, 0, 1 ], [ 0, 1, 0, 1 ], [ 0, 1, 0, 1 ] ]
[ [ 0, 1, 0, 1 ], [ 0, 1, 0, 1 ], [ 0, 1, 0, 1 ], [ 0, 1, 0, 1 ] ]
[ [ 1, 1, 0, 1 ], [ 1, 1, 0, 1 ], [ 1, 1, 0, 1 ], [ 1, 1, 0, 1 ] ]
[ [ 1, 1, 0, 1 ], [ 1, 1, 0, 1 ], [ 1, 1, 0, 1 ], [ 1, 1, 0, 1 ] ]

다음과 같이 나타나는 이유

fill(new Array(...)) 에서 생성된 배열의 element들이, 같은 주소 값의 배열로 채워져서
하나의 index를 변경하더라도 다같이 바뀌는 문제인 것으로 보였다.

const reportNestedArr = new Array(id_list.length).fill(new Array(id_list.length).fill(0))

해결방안

그냥 반복문 사용해서 하자

const reportNestedArr = []
for(let i in id_list){
    reportNestedArr.push(new Array(id_list.length).fill(0))
}

최종

function solution(id_list, report, k) {
    const reportNestedArr = [...Array(id_list.length)].map(() => Array(id_list.length).fill(0))
    const reportedNum = new Array(id_list.length).fill(0)
    const answer = new Array(id_list.length).fill(0)
    
    
    for(let i in report){
        const str = report[i].split(' ')
        const reporter = id_list.indexOf(str[0])
        const reported = id_list.indexOf(str[1])
        
        if(reportNestedArr[reporter][reported] === 0){
            reportNestedArr[reporter][reported]++
            reportedNum[reported]++
        }
    }
    
    for(let reported in reportedNum){
        if(reportedNum[reported] >= k){
            for(let reporter in reportNestedArr){
                if(reportNestedArr[reporter][reported] === 1){
                    answer[reporter]++
                }
            }
        }
    }
    
    return answer;
}

인상 깊었던 다른 사람의 풀이

function solution(id_list, report, k) {
  let reports = [...new Set(report)].map((a) => {
    return a.split(' ');
  });
  let counts = new Map();
  for (const bad of reports) {
    counts.set(bad[1], counts.get(bad[1]) + 1 || 1);
  }
  let good = new Map();
  for (const report of reports) {
    if (counts.get(report[1]) >= k) {
      good.set(report[0], good.get(report[0]) + 1 || 1);
    }
  }
  let answer = id_list.map((a) => good.get(a) || 0);
  return answer;
}

Set을 사용해 중복을 제거해준 것이 인상 깊었다.

또한, 불필요한 초기화 과정이 들어간다 싶으면
Map을 사용해, 그 과정을 생략하는 방법을 고려해볼 수도 있겠다는 생각이 들었다.

그리고 배열의 map 함수가 진짜 깡패구나 싶었다 ㅋㅋㅋ

0개의 댓글