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 e69b78f

Browse files
Merge pull request #3 from prove-ability/main
Add 2 answer
2 parents 43bc643 + 261bbf9 commit e69b78f

File tree

2 files changed

+95
-41
lines changed

2 files changed

+95
-41
lines changed

‎level-1/완주하지-못한-선수.js‎

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,64 @@
22
//완벽한 정답이 아닙니다.
33
//정답 1 - codeisneverodd
44
function solution(participant, completion) {
5-
var answer = '';
6-
participant = participant.sort()
7-
completion = completion.sort()
8-
for (let i = 0, len = completion.length; i < len; i++) {
9-
if (participant[i] !== completion[i]) return participant[i]
10-
}
11-
answer = participant[participant.length - 1]
12-
return answer
5+
var answer = "";
6+
participant = participant.sort();
7+
completion = completion.sort();
8+
for (let i = 0, len = completion.length; i < len; i++) {
9+
if (participant[i] !== completion[i]) return participant[i];
10+
}
11+
answer = participant[participant.length - 1];
12+
return answer;
1313
}
1414

1515
//정답 2 - jaewon1676
1616
function solution(participant, completion) {
17-
var answer = '';
18-
for (let i=0; i<participant.length; i++){
19-
for (let j=0; j<completion.length; j++){
20-
if (participant[i] === completion[j]) {
21-
console.log(participant,completion)
22-
participant.splice(i, 1)
23-
completion.splice(j, 1)
24-
i--;
25-
j--;
26-
console.log(participant,completion)
27-
break;
28-
}
29-
}
30-
}
31-
32-
return participant[0]
33-
}
17+
var answer = "";
18+
for (let i = 0; i < participant.length; i++) {
19+
for (let j = 0; j < completion.length; j++) {
20+
if (participant[i] === completion[j]) {
21+
console.log(participant, completion);
22+
participant.splice(i, 1);
23+
completion.splice(j, 1);
24+
i--;
25+
j--;
26+
console.log(participant, completion);
27+
break;
28+
}
29+
}
30+
}
31+
32+
return participant[0];
33+
}
34+
35+
//완벽한 정답이 아닙니다.
36+
//정답 3 - hyosung
37+
function solution(participant, completion) {
38+
let answer = "";
39+
// 2개 이상을 가진 특정값의 갯수 기록용 변수
40+
let max = 0;
41+
// 반복문 내부에서 set.has 를 사용하기 위해 Set 선언 (처음에는 Array.findIndex 를 사용)
42+
const set = new Set([...completion]);
43+
// 반복문 최적화 - 반복되던 연산 제거 (값 비교, length)
44+
const length = participant.length;
45+
for (let i = length; i--; ) {
46+
// 완주자 명단에 없다면 완주하지 못한 참가자 이므로 바로 종료
47+
if (!set.has(participant[i])) {
48+
answer = participant[i];
49+
break;
50+
}
51+
// 배열안에 특정값 갯수 확인
52+
let count = participant.reduce(
53+
(a, v) => (v === participant[i] ? a + 1 : a),
54+
0
55+
);
56+
// 해당 값이 참가자 그룹 내 2명 이상이고 이전 최대 동명이인 참가자보다 많다면
57+
// 해당 로직을 반복하면 제일 많은 동명이인을 알 수 있다
58+
if (count > 1 && max < count) {
59+
answer = participant[i];
60+
// 조건에 맞는 동명이인 수 저장
61+
max = count;
62+
}
63+
}
64+
return answer;
65+
}

‎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 によって変換されたページ (->オリジナル) /