Skip to content

Commit ebf901b

Browse files
committed
add (programmers - level1, 3 각 1문제): js 풀이
1 parent e8c8439 commit ebf901b

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

programmers/level1/모의고사.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
function solution(answers) {
2+
var answer = [];
3+
4+
// 1번: 1, 2, 3, 4, 5,
5+
// 6, 7, 8, 9, 10
6+
// 2번: 2, 1, 2, 3, 2, 4, 2, 5
7+
// a[0] = p2[0] 0 % 7 = 0
8+
// a[1] = p2[1] 1 % 7 = 1
9+
// a[8] = p2[0] 8 % 7 = 1 - 1
10+
// 3번: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5
11+
// a[0] = p3[0] = 0 & 9 = 0
12+
// a[1] = p3[1] = 1 % 9 = 1
13+
14+
// a[10] = p3[0] 10 % 9 - 1
15+
// a[11] = p3[1] 11 % 9 = 2 - 1 = 1
16+
// a[12] = p3[2] 12 % 9 = 3 - 1 = 2
17+
18+
const p2 = [2, 1, 2, 3, 2, 4, 2, 5];
19+
const p3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
20+
21+
const corrects = [0, 0, 0];
22+
23+
answers.forEach((ans, idx) => {
24+
if (ans === (idx % 5) + 1) {
25+
corrects[0] = corrects[0] + 1;
26+
}
27+
28+
if (ans === p2[idx % 8]) {
29+
corrects[1] = corrects[1] + 1;
30+
}
31+
32+
if (ans === p3[idx % 10]) {
33+
corrects[2] = corrects[2] + 1;
34+
}
35+
});
36+
37+
const max = Math.max(...corrects);
38+
39+
corrects.forEach((c, i) => {
40+
if (c === max) {
41+
answer.push(i + 1);
42+
}
43+
});
44+
45+
return answer;
46+
}
47+
48+
function solution2(answers) {
49+
let answer = [];
50+
51+
const num1 = [1, 2, 3, 4, 5];
52+
const num2 = [2, 1, 2, 3, 2, 4, 2, 5];
53+
const num3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
54+
55+
const score = new Map();
56+
score.set(1, 0);
57+
score.set(2, 0);
58+
score.set(3, 0);
59+
60+
answers.forEach((ans, idx) => {
61+
if (num1[idx % 5] === ans) {
62+
score.set(1, score.get(1) + 1);
63+
}
64+
65+
if (num2[idx % 8] === ans) {
66+
score.set(2, score.get(2) + 1);
67+
}
68+
69+
if (num3[idx % 10] === ans) {
70+
score.set(3, score.get(3) + 1);
71+
}
72+
});
73+
74+
const max = Math.max(...score.values());
75+
76+
for (const [key, value] of score.entries()) {
77+
if (value === max) {
78+
answer.push(key);
79+
}
80+
}
81+
return answer;
82+
}

programmers/level3/단속카메라.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function solution(routes) {
2+
let answer = 0;
3+
4+
routes.sort((a, b) => {
5+
if (a[0] !== b[0]) {
6+
return a[0] - b[0];
7+
} else {
8+
a[1] - b[1];
9+
}
10+
});
11+
12+
let prev = Number.NEGATIVE_INFINITY;
13+
for (const route of routes) {
14+
const [start, end] = route;
15+
16+
if (prev < start) {
17+
answer++;
18+
prev = end;
19+
continue;
20+
}
21+
22+
if (prev > end) {
23+
prev = end;
24+
}
25+
}
26+
return answer;
27+
}

0 commit comments

Comments
 (0)