Skip to content

Commit fe61a93

Browse files
author
gyeong-hyeon-kim
committed
Add 1 solution.
1 parent e1f136a commit fe61a93

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

level-2/메뉴-리뉴얼.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
function solution(orders, course) {
4+
var answer = [];
5+
for (const selectNum of course) {
6+
let combinations = []
7+
for (const order of orders) {
8+
getCombinations(Array.from(order), selectNum)
9+
.map(combination => combination.sort().join(''))// 'WX'는 'XW'와 같아야한다.
10+
.forEach(combString => combinations.push(combString))
11+
}
12+
const combCounts = combinations.reduce((counts, combination) => {
13+
counts[combination] = (counts[combination] || 0) + 1;
14+
return counts;
15+
}, {});
16+
let maxCount = 0, maxComb = []
17+
for (const comb in combCounts) if (combCounts[comb] >= maxCount) maxCount = combCounts[comb]
18+
for (const comb in combCounts) if (combCounts[comb] === maxCount && maxCount >= 2) maxComb.push(comb)
19+
answer.push(...maxComb)
20+
}
21+
answer = answer.sort()
22+
return answer;
23+
}
24+
25+
const getCombinations = (array, selectNum) => {
26+
const result = [];
27+
if (selectNum === 1) return array.map((element) => [element]);
28+
array.forEach((fixed, index, origin) => {
29+
const restCombinations = getCombinations(origin.slice(index + 1), selectNum - 1);
30+
const attached = restCombinations.map((restCombination) => [fixed, ...restCombination]);
31+
result.push(...attached);
32+
});
33+
return result;
34+
}

0 commit comments

Comments
 (0)