일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 715. Range Module
- Class
- Protocol
- attribute
- 315. Count of Smaller Numbers After Self
- Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- Python
- 30. Substring with Concatenation of All Words
- Regular Expression
- concurrency
- 시바견
- Python Implementation
- 운영체제
- shiba
- 프로그래머스
- kaggle
- 컴퓨터의 구조
- 파이썬
- DWG
- t1
- LeetCode
- 밴픽
- Substring with Concatenation of All Words
- Decorator
- iterator
- Python Code
- data science
- Generator
- 43. Multiply Strings
Archives
- Today
- Total
Scribbling
[Programmers] 네트워크 본문
https://school.programmers.co.kr/learn/courses/30/lessons/43162
1. Python
def solution(n, computers):
visited = set()
def dfs(i):
if i in visited:
return
visited.add(i)
for j in range(n):
if computers[i][j] == 1:
dfs(j)
ret = 0
for i in range(n):
if i not in visited:
dfs(i)
ret += 1
return ret
2. C++
void dfs(int n, const vector<vector<int>>& computers, set<int>& visited, int i) {
if (visited.find(i) != visited.end()) return;
visited.insert(i);
for (int j=0; j<n; j++) {
if (computers[i][j] == 1) {
dfs(n, computers, visited, j);
}
}
}
int solution(int n, vector<vector<int>> computers) {
int ret {0};
set<int> visited;
for (int i=0; i<n; i++) {
if (visited.find(i) == visited.end()) {
dfs(n, computers, visited, i);
ret++;
}
}
return ret;
}
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[Programmers] 단어 변환 (0) | 2024.06.20 |
---|---|
[Programmers] 게임 맵 최단거리 (0) | 2024.06.20 |
[Programmers] 타겟 넘버 (0) | 2024.06.20 |
[Programmers] 도둑질 (0) | 2024.06.18 |
[Programmers] 사칙연산 (0) | 2024.06.18 |