Skip to content

2022.03.25. Chaerin-dev 풀이 추가 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions level-1/x만큼-간격이-있는-n개의-숫자.js
Original file line number Diff line number Diff line change
@@ -2,9 +2,21 @@
//완벽한 정답이 아닙니다.
//정답 1 - codeisneverodd
function solution(x, n) {
var answer = [];
for (let i = 0; i < n; i++) {
answer[i] = x * (i + 1)
}
return answer;
}
var answer = [];
for (let i = 0; i < n; i++) {
answer[i] = x * (i + 1);
}
return answer;
}

//정답 2 - chaerin-dev
function solution(x, n) {
var answer = [];
let add_gap = x;
// n번 반복
for (let i = 0; i < n; i++) {
answer.push(x); // 처음 배열에 넣을 x
x += add_gap; // x를 배열에 넣은 후 x값을 add_gap만큼 증가
}
return answer;
}
120 changes: 70 additions & 50 deletions level-1/완주하지-못한-선수.js
Original file line number Diff line number Diff line change
@@ -2,64 +2,84 @@
//완벽한 정답이 아닙니다.
//정답 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;
}
}
}
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];
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;
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;
}

//완벽한 정답이 아닙니다.
//정답 4 - chaerin-dev
function solution(participant, completion) {
var answer = "";
// 두 배열을 정렬한다!
participant.sort();
completion.sort();
// 앞에서부터 차례로 비교하다가 값이 다를 때 participant의 요소가 완주하지 못한 선수!!
// if (participant[i] != completion[i] || i == participant.length - 1) 이런 식으로
// 완주하지 못한 선수의 이름이 마지막에 있을 경우도 고려해야 하나..? 라고 생각했지만
// 그 때는 completion[i]의 값이 undefined가 되므로 괜찮음!
for (let i = 0; i < participant.length; i++) {
if (participant[i] != completion[i]) {
answer = participant[i];
break;
}
}
return answer;
}
20 changes: 20 additions & 0 deletions level-1/직사각형-별찍기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//https://github.com/codeisneverodd/programmers-coding-test
//완벽한 정답이 아닙니다.
//정답 1 - chaerin-dev
process.stdin.setEncoding("utf8");
process.stdin.on("data", (data) => {
const n = data.split(" ");
const a = Number(n[0]),
b = Number(n[1]);
// 정답을 저장할 문자열 answer
answer = "";
// 세로 길이만큼 아래의 과정(가로 길이만큼 별 더해주고 줄 바꾸기) 반복
for (let i = 0; i < b; i++) {
// 가로 길이만큼 문자열에 별 더해주기
for (let j = 0; j < a; j++) answer += "*";
// 가로 길이만큼 별을 다 더해줬으면 줄 바꾸기
answer += "\n";
}
// 정답 출력
console.log(answer);
});
41 changes: 27 additions & 14 deletions level-1/행렬의-덧셈.js
Original file line number Diff line number Diff line change
@@ -2,23 +2,36 @@
//완벽한 정답이 아닙니다.
//정답 1 - codeisneverodd
function solution(arr1, arr2) {
var answer = [];
arr1.forEach((row, rowIndex) => {
answer.push(row.map((col, colIndex) => col + arr2[rowIndex][colIndex]))
})
return answer;
var answer = [];
arr1.forEach((row, rowIndex) => {
answer.push(row.map((col, colIndex) => col + arr2[rowIndex][colIndex]));
});
return answer;
}

//정답 2 - codeisneverodd
function solution(arr1, arr2) {
var answer = new Array(arr1.length)
for (let i = 0; i < arr1.length; i++) {
answer[i] = new Array(arr1[0].length)
var answer = new Array(arr1.length);
for (let i = 0; i < arr1.length; i++) {
answer[i] = new Array(arr1[0].length);
}
for (let row = 0; row < answer.length; row++) {
for (let col = 0; col < answer[0].length; col++) {
answer[row][col] = arr1[row][col] + arr2[row][col];
}
for (let row = 0; row < answer.length; row++) {
for (let col = 0; col < answer[0].length; col++) {
answer[row][col] = arr1[row][col] + arr2[row][col]
}
}
return answer;
}

//정답 3 - chaerin-dev
function solution(arr1, arr2) {
var answer = [];
for (let i = 0; i < arr1.length; i++) {
let ans_row = [];
for (let j = 0; j < arr1[0].length; j++) {
ans_row.push(arr1[i][j] + arr2[i][j]);
}
return answer;
}
answer.push(ans_row);
}
return answer;
}