위장
2020.07.31
const reduceHelper = (acc, [cloth, category]) => {
if (!acc.hasOwnProperty(category)) {
acc[category] = [cloth];
} else {
acc[category].push(cloth);
}
return acc;
};
const solution = (clothes) => {
const hash = clothes.reduce(reduceHelper, {});
let result = 1;
for (const category in hash) {
result *= hash[category].length + 1;
}
return result - 1;
};
- 너무 풀기만 하면 그만이라는 식으로 짠 것 같아서 리팩토링 해보았다.
const reduceHelper1 = (acc, [cloth, category], idx, origin) => {
if (!acc.hasOwnProperty(category)) {
acc[category] = 1;
} else {
acc[category]++;
}
if (idx == origin.length - 1) {
return Object.values(acc);
}
return acc;
};
const reduceHelper2 = (acc, current, origin, length) => (acc *= current + 1);
const solution = (clothes) => {
return clothes.reduce(reduceHelper1, {}).reduce(reduceHelper2, 1) - 1;
};