일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- t1
- Regular Expression
- 프로그래머스
- Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- 컴퓨터의 구조
- shiba
- iterator
- 715. Range Module
- 파이썬
- Protocol
- concurrency
- 시바견
- 밴픽
- kaggle
- 운영체제
- Python Implementation
- Class
- Python Code
- DWG
- 109. Convert Sorted List to Binary Search Tree
- LeetCode
- attribute
- data science
- 43. Multiply Strings
- Convert Sorted List to Binary Search Tree
- Generator
- Decorator
- Python
Archives
- Today
- Total
Scribbling
[Programmers] 가장 먼 노드 본문
https://school.programmers.co.kr/learn/courses/30/lessons/49189
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. Python
from collections import deque
def solution(n, edge):
graph = [[] for _ in range(n)]
for u, v in edge:
u -= 1
v -= 1
graph[u].append(v)
graph[v].append(u)
q = deque()
q.append((0, 0))
visited = set()
visited.add(0)
ret = 0
dist = -1
while q:
dist_u, u = q.popleft()
if dist_u > dist:
ret, dist = 0, dist_u
ret += 1
for v in graph[u]:
if v not in visited:
q.append((dist_u + 1, v))
visited.add(v)
return ret
2. C++
#include <string>
#include <vector>
#include <set>
#include <deque>
using namespace std;
int solution(int n, vector<vector<int>> edge) {
vector<vector<int>> graph (n, vector<int>());
for (auto vec : edge) {
int u = vec[0], v = vec[1];
u--; v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
deque<pair<int, int>> q;
q.push_back({0, 0});
set<int> visited;
visited.insert(0);
int ret = 0;
int dist = -1;
while (not q.empty()) {
auto[dist_u, u] = q.front();
q.pop_front();
if (dist_u > dist) {
ret = 0;
dist = dist_u;
}
ret++;
for (int v : graph[u]) {
if (visited.find(v) == visited.end()) {
q.push_back({dist_u+1, v});
visited.insert(v);
}
}
}
return ret;
}
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[Programmers] 방의 개수 (0) | 2024.07.02 |
---|---|
[Programmers] 순위 (0) | 2024.06.28 |
[Programmers] 입국심사 (0) | 2024.06.27 |
[Programmers] 징검다리 (0) | 2024.06.27 |
[Programmers] 여행경로 (0) | 2024.06.24 |