Skip to content

Commit 31ba9a7

Browse files
authored
Merge pull request #58 from boost-algorithm/doyeon
week15 문제 풀이
2 parents 4b8737d + ab0554e commit 31ba9a7

4 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const solution = (n, computers) => {
2+
let answer = 0;
3+
const network = Array.from({ length: n }, () => 0);
4+
5+
for (let i = 0; i < n; i += 1) {
6+
if (network[i] !== 0) continue;
7+
answer += 1;
8+
network[i] = answer;
9+
10+
const queue = [i];
11+
while (queue.length > 0) {
12+
const visit = queue.pop();
13+
computers[visit].forEach((computer, index) => {
14+
if (computer && network[index] === 0) {
15+
queue.push(index);
16+
network[index] = answer;
17+
}
18+
});
19+
}
20+
}
21+
22+
return answer;
23+
};

src/do02reen24/Review/week15.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# :fire: week15
2+
3+
## :ballot_box_with_check: 프로그래머스 위장(42578)
4+
5+
- 조합의 개념을 사용하여 풀 수 있었다.
6+
- 해당 옷을 아예 고르지 않는 경우도 있으므로 각 아이템 길이에 + 1 을 해주었다.
7+
- 모든 옷을 안고르는 경우는 제외해야하므로 정답에서 - 1 을 해주었다.
8+
9+
## :ballot_box_with_check: 프로그래머스 영어 끝말잇기(12981)
10+
11+
- 각 경우를 고려하여 처리해주었다.
12+
13+
## :ballot_box_with_check: 프로그래머스 네트워크(43162)
14+
15+
- BFS의 개념을 활용하여 풀었다.
16+
- 백준 2644번 문제와 비슷한 것 같다.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const failUser = (n, index) => {
2+
let person = index % n;
3+
let order = Math.floor(index / n) + 1;
4+
if (person === 0) {
5+
person = n;
6+
order -= 1;
7+
}
8+
return [person, order];
9+
};
10+
11+
const getLastChar = (word) => word.charAt(word.length - 1);
12+
13+
const solution = (n, words) => {
14+
const dictionary = {};
15+
let lastChar = words[0][0];
16+
for (let index = 0; index < words.length; index += 1) {
17+
const word = words[index];
18+
if (lastChar === word[0] && dictionary[word] === undefined) {
19+
dictionary[word] = true;
20+
lastChar = getLastChar(word);
21+
continue;
22+
}
23+
return failUser(n, index + 1);
24+
}
25+
26+
return [0, 0];
27+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const getCombinationNumber = (object) => {
2+
let combination = 1;
3+
for (const key in object) {
4+
const itemLength = object[key].length + 1;
5+
combination *= itemLength;
6+
}
7+
return combination;
8+
};
9+
10+
const solution = (clothes) => {
11+
const closet = {};
12+
clothes.forEach(([cloth, clothType]) => {
13+
closet[clothType]
14+
? closet[clothType].push(cloth)
15+
: (closet[clothType] = [cloth]);
16+
});
17+
18+
return getCombinationNumber(closet) - 1;
19+
};

0 commit comments

Comments
 (0)