티스토리 뷰
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
'코딩테스트' 카테고리의 다른 글
코딩테스트 연습 > 2017 팁스타운 > 짝지어 제거하기 (0) | 2020.08.25 |
---|---|
코딩테스트 연습 > 연습문제 > N개의 최소공배수 (0) | 2020.08.24 |
코딩테스트 연습 > 탐욕법(Greedy) > 구명보트 (0) | 2020.08.20 |
코딩테스트 연습 > 완전탐색 > 카펫 (0) | 2020.08.20 |
코딩테스트 연습 > 정렬 > H-Index (0) | 2020.08.19 |
댓글
공지사항