Algoritm & DataStructure
[PS - 트리] LeetCode 104. Maximum Depth of Binary Tree
[PS - 트리] LeetCode 104. Maximum Depth of Binary Tree
2022.09.30https://leetcode.com/problems/maximum-depth-of-binary-tree/ Maximum Depth of Binary Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com - 큐를 사용한 BFS로 구현 - 최초 Node가 nil인 경우 처리 - 한번 반복 될때마다 깊이를 +1 카운트 - 현재 큐의 길이(같은 레벨에 위치한 노드의 전체 개수)만큼 다시 추가해주는 작업 class Solution { func maxDepth(_ ..
[프로그래머스] 배달 / 다익스트라(BFS)
[프로그래머스] 배달 / 다익스트라(BFS)
2021.06.28import collections import sys import heapq def solution(N, road, K): graph = collections.defaultdict(list) result = collections.defaultdict() for i in range(1, N+1): result[i] = 500001 result[1] = 0 for u, v, t in road: graph[u].append((v,t)) graph[v].append((u,t)) print(graph) Q = [(1, 0)] dist = collections.defaultdict(int) answer = 0 while Q: node, time = heapq.heappop(Q) for v, t in graph[no..