문제 풀이 코드
function solution(clothes) {
var answer = 1;
var clothesDict = {}
for (var i = 0; i < clothes.length; i++){
if (clothes[i][1] in clothesDict){ // #1
clothesDict[clothes[i][1]] += 1;
}else{
clothesDict[clothes[i][1]] = 1;
}
}
for (var key in clothesDict){ // #2
answer *= clothesDict[key] + 1
}
return answer - 1; // #3
}
#1
만약 clothesDict 에 없는 key 값이 들어왔다면 1로 초기화 시키고, 존재한다면 1 을 더해줘 해당 의상 종류가 몇개가 있는지 저장합니다.
#2
의상 갯수 + 입지 않았을 때의 경우를 clothesDict[key] + 1
로 표현했다.
#3
아무것도 입지 않았을 경우를 제외하기 위해 1을 빼주었다.
'코딩테스트' 카테고리의 다른 글
[코딩테스트] 프로그래머스 프린터 with Python (0) | 2021.05.10 |
---|---|
[코딩테스트] 프로그래머스 다리를 지나는 트럭 with Python (0) | 2021.05.09 |
[코딩테스트] 프로그래머스 기능개발 with javascript (0) | 2021.05.07 |
[코딩테스트] 프로그래머스 체육복 with javascript (0) | 2021.05.06 |
[코딩테스트] 프로그래머스 베스트앨범 with javascript (0) | 2021.05.06 |