Skip to content

Commit

Permalink
add (programmers - level1, 3 각 1문제): js 풀이
Browse files Browse the repository at this point in the history
  • Loading branch information
padosum committed Feb 1, 2023
1 parent e8c8439 commit ebf901b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
82 changes: 82 additions & 0 deletions programmers/level1/모의고사.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function solution(answers) {
var answer = [];

// 1번: 1, 2, 3, 4, 5,
// 6, 7, 8, 9, 10
// 2번: 2, 1, 2, 3, 2, 4, 2, 5
// a[0] = p2[0] 0 % 7 = 0
// a[1] = p2[1] 1 % 7 = 1
// a[8] = p2[0] 8 % 7 = 1 - 1
// 3번: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5
// a[0] = p3[0] = 0 & 9 = 0
// a[1] = p3[1] = 1 % 9 = 1

// a[10] = p3[0] 10 % 9 - 1
// a[11] = p3[1] 11 % 9 = 2 - 1 = 1
// a[12] = p3[2] 12 % 9 = 3 - 1 = 2

const p2 = [2, 1, 2, 3, 2, 4, 2, 5];
const p3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

const corrects = [0, 0, 0];

answers.forEach((ans, idx) => {
if (ans === (idx % 5) + 1) {
corrects[0] = corrects[0] + 1;
}

if (ans === p2[idx % 8]) {
corrects[1] = corrects[1] + 1;
}

if (ans === p3[idx % 10]) {
corrects[2] = corrects[2] + 1;
}
});

const max = Math.max(...corrects);

corrects.forEach((c, i) => {
if (c === max) {
answer.push(i + 1);
}
});

return answer;
}

function solution2(answers) {
let answer = [];

const num1 = [1, 2, 3, 4, 5];
const num2 = [2, 1, 2, 3, 2, 4, 2, 5];
const num3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

const score = new Map();
score.set(1, 0);
score.set(2, 0);
score.set(3, 0);

answers.forEach((ans, idx) => {
if (num1[idx % 5] === ans) {
score.set(1, score.get(1) + 1);
}

if (num2[idx % 8] === ans) {
score.set(2, score.get(2) + 1);
}

if (num3[idx % 10] === ans) {
score.set(3, score.get(3) + 1);
}
});

const max = Math.max(...score.values());

for (const [key, value] of score.entries()) {
if (value === max) {
answer.push(key);
}
}
return answer;
}
27 changes: 27 additions & 0 deletions programmers/level3/단속카메라.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function solution(routes) {
let answer = 0;

routes.sort((a, b) => {
if (a[0] !== b[0]) {
return a[0] - b[0];
} else {
a[1] - b[1];
}
});

let prev = Number.NEGATIVE_INFINITY;
for (const route of routes) {
const [start, end] = route;

if (prev < start) {
answer++;
prev = end;
continue;
}

if (prev > end) {
prev = end;
}
}
return answer;
}

0 comments on commit ebf901b

Please sign in to comment.