코딩테스트
코딩테스트 연습 > 해시 > 위장
안양사람
2020. 8. 20. 23:54
728x90
SMALL
처음푼 코드
function solution(clothes) {
const kinds=[...new Set(clothes.map(clothe=>clothe[1]))];
let arr=[];
for(let j=0;j<kinds.length;j++){
let num=0;
for(let i=0;i<clothes.length;i++){
if(kinds[j]===clothes[i][1]){
num++;
}
}
arr.push(num);
}
let ans=1;
for(let i=0;i<arr.length;i++){
ans=ans*(arr[i]+1);
}
return ans-1;
}
다른사람코드
function solution(clothes) {
return Object.values(clothes.reduce((obj, t)=> {
obj[t[1]] = obj[t[1]] ? obj[t[1]] + 1 : 1;
return obj;
} , {})).reduce((a,b)=> a*(b+1), 1)-1;
}
3번째줄을 이해가 처음에 안됬다. 알고보니 =은 연산순번이 느려서 =다음부터 괄호가 쳐져있다고 생각하면 된다고 했다.
728x90
LIST