Skip to content

Commit 144e43f

Browse files
committed
15회차 readme 추가
1 parent b83a89a commit 144e43f

File tree

7 files changed

+174
-0
lines changed

7 files changed

+174
-0
lines changed
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);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 12981 영어 끝말잇기 - 성공
2+
3+
## 아이디어 구현
4+
5+
단순 구현 문제
6+
7+
words를 돌면서 조건에 맞는것 리턴 해주면 됨.
8+
9+
[번호, 차례]를 잘 출력하는게 까다로웠음.
10+
11+
차례는 항상 올림으로 해주면 맞게 된다.
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));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 42578 위장 - 성공
2+
3+
## 아이디어 구현
4+
5+
이 문제가 왜 해시인가? 딕셔너리 아닌가? 했더니
6+
7+
해시맵 === 딕셔너리 네요. 같은 자료구조인데 여러가지로 불리는.
8+
9+
[키기반의 컬렉션](https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Keyed_collections#Maps) 참고해서, map 써서도 구현해봤습니다
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function solution(clothes) {
2+
let answer = 1;
3+
console.log(clothes);
4+
5+
const map = new Map();
6+
7+
clothes.forEach((cloth) => {
8+
if (!map.get(cloth[1])) {
9+
return map.set(cloth[1], [cloth[0]]);
10+
}
11+
12+
map.get(cloth[1]).push(cloth[0]);
13+
});
14+
15+
for (let key of map.keys()) {
16+
answer *= map.get(key).length + 1;
17+
}
18+
answer -= 1;
19+
20+
return answer;
21+
}
22+
23+
const clothes = [
24+
['yellow_hat', 'headgear'],
25+
['blue_sunglasses', 'eyewear'],
26+
['green_turban', 'headgear'],
27+
];
28+
29+
console.log(solution(clothes));
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);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 43162 네트워크 - 성공
2+
3+
## BFS
4+
5+
bfs에서 최단거리를 구할 필요는 없고, 지나간 모든 경로를 visited에 담아서 반환하면 이걸 하나의 네트워크라고 생각한다.
6+
7+
각각의 컴퓨터가 root일때 네트워크를 구하고 겹치지 않는 네트워크의 개수를 구해주면 된다.
8+
9+
겹치지 않도록 하기위해 bfs로 root를 넣기 전, network를 모두 체크해서 겹치는 root가 있으면 같은 네트워크가 이미 있다는 것이므로 pass 했다.
10+
11+
위 조건에 따른 모든 network를 구한 후 개수를 세면 끝.

0 commit comments

Comments
 (0)