Skip to content

Commit d1c9fff

Browse files
committed
15주차 문제풀이
1 parent c9d5882 commit d1c9fff

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

src/grap3fruit/js/goorm/12981.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const check = (words, n, word, idx, visited) => {
2+
if (visited.includes(word)) {
3+
return [(idx % n) + 1, Math.ceil((idx + 1) / n)];
4+
}
5+
if (idx > 0) {
6+
if (words[idx - 1][words[idx - 1].length - 1] !== word[0]) {
7+
return [(idx % n) + 1, Math.ceil((idx + 1) / n)];
8+
}
9+
}
10+
return null;
11+
};
12+
13+
function solution(n, words) {
14+
let answer = null;
15+
const visited = [];
16+
17+
words.forEach((word, idx) => {
18+
if (!answer) {
19+
answer = check(words, n, word, idx, visited);
20+
visited.push(word);
21+
}
22+
});
23+
if (!answer) {
24+
answer = [0, 0];
25+
}
26+
return answer;
27+
}
28+
29+
const n = 2;
30+
const words = ['hello', 'two'];
31+
// const words = ['hello', 'one', 'even', 'never', 'now', 'world', 'draw'];
32+
// const words = ['tank', 'kick', 'know', 'wheel', 'land', 'dream', 'mother', 'robot', 'tank'];
33+
34+
solution(n, words);

src/grap3fruit/js/goorm/42578.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function solution(clothes) {
2+
let answer = 1;
3+
const data = {};
4+
5+
clothes.forEach((cloth) => {
6+
if (!data[cloth[1]]) {
7+
return (data[cloth[1]] = [cloth[0]]);
8+
}
9+
data[cloth[1]].push(cloth[0]);
10+
});
11+
12+
for (const el in data) {
13+
answer *= data[el].length + 1;
14+
}
15+
answer -= 1;
16+
17+
return answer;
18+
}
19+
20+
const clothes = [
21+
['yellow_hat', 'headgear'],
22+
['blue_sunglasses', 'eyewear'],
23+
['green_turban', 'headgear'],
24+
];
25+
26+
console.log(solution(clothes));

src/grap3fruit/js/goorm/43162.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const bfs = (computers, root) => {
2+
const visited = [root];
3+
let q = [computers[root]];
4+
5+
while (q.length > 0) {
6+
let item = q.shift();
7+
8+
item.forEach((el, idx) => {
9+
if (el === 1 && !visited.includes(idx)) {
10+
q.push(computers[idx]);
11+
visited.push(idx);
12+
}
13+
});
14+
}
15+
16+
return visited;
17+
};
18+
19+
function solution(n, computers) {
20+
let answer = 0;
21+
const visited = [];
22+
23+
computers.forEach((_, idx) => {
24+
let visitFlag = false;
25+
visited.forEach((el) => {
26+
if (el.includes(idx)) {
27+
visitFlag = true;
28+
}
29+
});
30+
31+
if (!visitFlag) {
32+
const newVisited = bfs(computers, idx);
33+
visited.push(newVisited);
34+
}
35+
});
36+
37+
console.log(visited.length);
38+
answer = visited.length;
39+
return answer;
40+
}
41+
42+
const n = 3;
43+
const computers = [
44+
[1, 1, 0],
45+
[1, 1, 0],
46+
[0, 0, 1],
47+
];
48+
// const computers = [
49+
// [1, 1, 0],
50+
// [1, 1, 1],
51+
// [0, 1, 1],
52+
// ];
53+
54+
solution(n, computers);

0 commit comments

Comments
 (0)