HelloMinchan
처음처럼
HelloMinchan
LinkedIn
전체 방문자
오늘
어제
  • 분류 전체보기 (306)
    • Backend (4)
      • NestJS (1)
      • Express (1)
      • Spring (2)
    • Infrastructure (1)
      • AWS (1)
    • Frontend (1)
      • Next.js (1)
    • Language & Runtime (4)
      • Java (2)
      • Node.js (2)
    • Computer Science (8)
      • Computer Networks (3)
      • Operating Systems (4)
      • OOP (1)
    • 독서 (4)
      • 데이터 중심 애플리케이션 설계 (3)
      • 객체지향의 사실과 오해 (1)
    • 회고 (4)
      • Project (2)
      • Career (2)
    • Deprecated (280)

채널

  • GitHub
  • LinkedIn

최근 글

태그

  • 프로그래밍
  • 백준Python
  • 백준
  • 코딩
  • 백준Go
  • front-end
  • 개발자
  • 프로그래머스C++
  • Database
  • 백준C++
  • 백엔드
  • 프로그래머스
  • Algospot
  • 데이터베이스
  • Baekjoon Online Judge
  • 알고스팟Python
  • back-end
  • programmers
  • 알고스팟
  • 프로그래머스Python

최근 댓글

인기 글

hELLO
HelloMinchan

처음처럼

[programmers] 프로그래머스 가장 먼 노드(Python)
Deprecated

[programmers] 프로그래머스 가장 먼 노드(Python)

2020. 6. 10. 23:47

(주)그렙

[programmers] 프로그래머스 가장 먼 노드

(Python)

(글쓴날 : 2020.06.10)

 


* programmers, 프로그래머스 문제 Python 언어 풀이입니다.

* 소스 코드의 저작권은 글쓴이에게 있습니다.


 

 

프로그래머스 가장 먼 노드


1) 문제

문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/49189

 

코딩테스트 연습 - 가장 먼 노드

6 [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]] 3

programmers.co.kr


2) 풀이 과정

* 시간 복잡도 : O(E log V)

 

n개의 노드가 있는 그래프가 주어질 때, 1번 노드에서 제일 멀리 떨어진 노드가 몇 개인지 구하는 문제입니다.

 

저의 경우, 다익스트라를 적용하였고, Python을 사용했습니다.

주어지는 간선에 임의적으로 가중치를 1씩 부여한 무향 인접 리스트를 구현한 뒤, 다익스트라를 적용하여 문제를 해결했습니다.


3) 코드

 

* Python 코드

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
32
33
34
import heapq
 
def BFS(hq, di, adjList):
    while(hq):
        wei, vec = heapq.heappop(hq)
        
        if di[vec] > wei:
            di[vec] = wei
            
            for w, v in adjList[vec]:
                heapq.heappush(hq, (w+wei,v))
        
def solution(n, edge):
    adjList = [[] for _ in range(n+1)]
    INF = 2147483647
    di = [INF] * (n + 1)
    di[1] = 0
    hq = []
    
    for e in edge:
        adjList[e[0]].append((1,e[1]))
        adjList[e[1]].append((1,e[0]))
    
    for w, v in adjList[1]:
        heapq.heappush(hq,(w,v))
    
    BFS(hq, di, adjList)
    
    maxDistance = 0
    for d in di:
        if d != INF and maxDistance < d:
            maxDistance = d
    
    return di.count(maxDistance)

 

저작자표시 비영리 변경금지 (새창열림)

'Deprecated' 카테고리의 다른 글

[programmers] 프로그래머스 가운데 글자 가져오기(Python)  (0) 2020.06.11
[programmers] 프로그래머스 탑(Python)  (0) 2020.06.11
[Baekjoon Online Judge] 백준 1261번 알고스팟(Python)  (0) 2020.06.10
[Baekjoon Online Judge] 백준 1238번 파티(Python)  (0) 2020.06.10
[Baekjoon Online Judge] 백준 2468번 안전 영역(Python)  (0) 2020.06.10
    'Deprecated' 카테고리의 다른 글
    • [programmers] 프로그래머스 가운데 글자 가져오기(Python)
    • [programmers] 프로그래머스 탑(Python)
    • [Baekjoon Online Judge] 백준 1261번 알고스팟(Python)
    • [Baekjoon Online Judge] 백준 1238번 파티(Python)
    HelloMinchan
    HelloMinchan
    Though you should not fear failure, You should do your very best to avoid it.

    티스토리툴바