Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- MVC
- Algorithm
- 파이팅
- 1302
- 1697
- 디자인패턴
- 최소힙
- Heap
- 12865
- 프로그래머스
- 백준
- AOS
- 올바른 괄호
- 백준 #알고리즘 # Algorithm #파이썬
- Python
- 1766
- python #7490 #백준 #알고리즘 #BFS
- 라이징프로그래머2 #Android #안드로이드 #Quitter #MakeUs
- deque
- 1759
- CS
- REST API
- level3
- 1715
- 해커랭크
- 파이썬
- 안드로이드
- BFS
- 2941
- 알고리즘
Archives
Liam 일지
HackerRank 본문
728x90
Bu----- 채용 코딩테스트문제
문제: 주어진 start->end 리스트로 연결된 각 그래프의 노드 수의 루트값의 올림한 값을 더한 결과를 나타내라.
from collections import deque
import math
def connectedSum(graph_nodes, graph_from, graph_to):
graph =[[] for i in range(graph_nodes+1)]
result=0
visit=[False]*(graph_nodes+1)
for i in range(len(graph_from)):
graph[graph_from[i]].append(graph_to[i])
graph[graph_to[i]].append(graph_from[i])
q=deque()
for i in graph_from:
cnt=1
if visit[i]==False:
visit[i]=True
q.append(i)
while q:
now=q.popleft()
for j in graph[now]:
if visit[j]==False:
visit[j]=True
cnt+=1
q.append(j)
result+=int(math.ceil(math.sqrt(cnt)))
cnt=0
for i in range(1,graph_nodes+1):
if visit[i]==False:
cnt+=1
result+=cnt
print(result)
graph_nodes=4
graph_from=[1,1]
graph_to=[2,4]
connectedSum(graph_nodes,graph_from,graph_to)
'Algorithm(백준)' 카테고리의 다른 글
| 백준 1926번 그림 (0) | 2021.05.10 |
|---|---|
| 프로그래머스 올바른 괄호 (0) | 2021.05.10 |
| 백준 2644번 촌수계산 (0) | 2021.04.30 |
| Hacker Rank (Climbing the Leaderboard) (0) | 2021.04.30 |
| 배열 평탄화 (0) | 2021.04.29 |