Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 57 additions & 25 deletions level-1/완주하지-못한-선수.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,64 @@
//완벽한 정답이 아닙니다.
//정답 1 - codeisneverodd
function solution(participant, completion) {
var answer = '';
participant = participant.sort()
completion = completion.sort()
for (let i = 0, len = completion.length; i < len; i++) {
if (participant[i] !== completion[i]) return participant[i]
}
answer = participant[participant.length - 1]
return answer
var answer = "";
participant = participant.sort();
completion = completion.sort();
for (let i = 0, len = completion.length; i < len; i++) {
if (participant[i] !== completion[i]) return participant[i];
}
answer = participant[participant.length - 1];
return answer;
}

//정답 2 - jaewon1676
function solution(participant, completion) {
var answer = '';
for (let i=0; i<participant.length; i++){
for (let j=0; j<completion.length; j++){
if (participant[i] === completion[j]) {
console.log(participant,completion)
participant.splice(i, 1)
completion.splice(j, 1)
i--;
j--;
console.log(participant,completion)
break;
}
}
}

return participant[0]
}
var answer = "";
for (let i = 0; i < participant.length; i++) {
for (let j = 0; j < completion.length; j++) {
if (participant[i] === completion[j]) {
console.log(participant, completion);
participant.splice(i, 1);
completion.splice(j, 1);
i--;
j--;
console.log(participant, completion);
break;
}
}
}

return participant[0];
}

//완벽한 정답이 아닙니다.
//정답 3 - hyosung
function solution(participant, completion) {
let answer = "";
// 2개 이상을 가진 특정값의 갯수 기록용 변수
let max = 0;
// 반복문 내부에서 set.has 를 사용하기 위해 Set 선언 (처음에는 Array.findIndex 를 사용)
const set = new Set([...completion]);
// 반복문 최적화 - 반복되던 연산 제거 (값 비교, length)
const length = participant.length;
for (let i = length; i--; ) {
// 완주자 명단에 없다면 완주하지 못한 참가자 이므로 바로 종료
if (!set.has(participant[i])) {
answer = participant[i];
break;
}
// 배열안에 특정값 갯수 확인
let count = participant.reduce(
(a, v) => (v === participant[i] ? a + 1 : a),
0
);
// 해당 값이 참가자 그룹 내 2명 이상이고 이전 최대 동명이인 참가자보다 많다면
// 해당 로직을 반복하면 제일 많은 동명이인을 알 수 있다
if (count > 1 && max < count) {
answer = participant[i];
// 조건에 맞는 동명이인 수 저장
max = count;
}
}
return answer;
}
54 changes: 38 additions & 16 deletions level-2/위장.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,45 @@
//완벽한 정답이 아닙니다.
//정답 1 - codeisneverodd
function solution(clothes) {
var answer = 1;
const spyWear = {}
for (const clothNPart of clothes) spyWear[clothNPart[1]] = (spyWear[clothNPart[1]] || 0) + 1
for (const part in spyWear) answer *= (spyWear[part] + 1)
return answer - 1;
var answer = 1;
const spyWear = {};
for (const clothNPart of clothes)
spyWear[clothNPart[1]] = (spyWear[clothNPart[1]] || 0) + 1;
for (const part in spyWear) answer *= spyWear[part] + 1;
return answer - 1;
}

//정답 2 - codeisneverodd
function solution(clothes) {
var answer = 0;
const spyWear = {}
for (const clothNPart of clothes) {
if (spyWear[clothNPart[1]] === undefined) spyWear[clothNPart[1]] = []
spyWear[clothNPart[1]].push(clothNPart[0])
}
const clothesCount = []
for (const part in spyWear) clothesCount.push(spyWear[part].length + 1)
answer = clothesCount.reduce((previous, current) => previous * current, 1) - 1
return answer;
}
var answer = 0;
const spyWear = {};
for (const clothNPart of clothes) {
if (spyWear[clothNPart[1]] === undefined) spyWear[clothNPart[1]] = [];
spyWear[clothNPart[1]].push(clothNPart[0]);
}
const clothesCount = [];
for (const part in spyWear) clothesCount.push(spyWear[part].length + 1);
answer =
clothesCount.reduce((previous, current) => previous * current, 1) - 1;
return answer;
}
// 정답 3 - hyosung
function solution(clothes) {
let answer = 1;
// 옷 종류
const types = {};
// 반복문 최적화 - length, 비교연산 제거
const length = clothes.length;
for (let i = length; i--; ) {
// 해당 옷의 종류가 없다면 종류 1
if (!types[clothes[i][1]]) types[clothes[i][1]] = 1;
// 해당 옷의 종류가 있다면 종류 증가
else types[clothes[i][1]] += 1;
}
// (종류 별 값 + 1 ) 을 다 곱셈
Object.values(types).forEach((v) => {
answer *= v + 1;
});

return answer - 1;
}