Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 261bbf9

Browse files
Add answer '위장'
1 parent 039d400 commit 261bbf9

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

‎level-2/위장.js‎

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,45 @@
22
//완벽한 정답이 아닙니다.
33
//정답 1 - codeisneverodd
44
function solution(clothes) {
5-
var answer = 1;
6-
const spyWear = {}
7-
for (const clothNPart of clothes) spyWear[clothNPart[1]] = (spyWear[clothNPart[1]] || 0) + 1
8-
for (const part in spyWear) answer *= (spyWear[part] + 1)
9-
return answer - 1;
5+
var answer = 1;
6+
const spyWear = {};
7+
for (const clothNPart of clothes)
8+
spyWear[clothNPart[1]] = (spyWear[clothNPart[1]] || 0) + 1;
9+
for (const part in spyWear) answer *= spyWear[part] + 1;
10+
return answer - 1;
1011
}
1112

1213
//정답 2 - codeisneverodd
1314
function solution(clothes) {
14-
var answer = 0;
15-
const spyWear = {}
16-
for (const clothNPart of clothes) {
17-
if (spyWear[clothNPart[1]] === undefined) spyWear[clothNPart[1]] = []
18-
spyWear[clothNPart[1]].push(clothNPart[0])
19-
}
20-
const clothesCount = []
21-
for (const part in spyWear) clothesCount.push(spyWear[part].length + 1)
22-
answer = clothesCount.reduce((previous, current) => previous * current, 1) - 1
23-
return answer;
24-
}
15+
var answer = 0;
16+
const spyWear = {};
17+
for (const clothNPart of clothes) {
18+
if (spyWear[clothNPart[1]] === undefined) spyWear[clothNPart[1]] = [];
19+
spyWear[clothNPart[1]].push(clothNPart[0]);
20+
}
21+
const clothesCount = [];
22+
for (const part in spyWear) clothesCount.push(spyWear[part].length + 1);
23+
answer =
24+
clothesCount.reduce((previous, current) => previous * current, 1) - 1;
25+
return answer;
26+
}
27+
// 정답 3 - hyosung
28+
function solution(clothes) {
29+
let answer = 1;
30+
// 옷 종류
31+
const types = {};
32+
// 반복문 최적화 - length, 비교연산 제거
33+
const length = clothes.length;
34+
for (let i = length; i--; ) {
35+
// 해당 옷의 종류가 없다면 종류 1
36+
if (!types[clothes[i][1]]) types[clothes[i][1]] = 1;
37+
// 해당 옷의 종류가 있다면 종류 증가
38+
else types[clothes[i][1]] += 1;
39+
}
40+
// (종류 별 값 + 1 ) 을 다 곱셈
41+
Object.values(types).forEach((v) => {
42+
answer *= v + 1;
43+
});
44+
45+
return answer - 1;
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /