def solution(queue1, queue2):
answer = 0
sum_q1 = sum(queue1)
sum_q2 = sum(queue2)
total = (sum_q1 + sum_q2) // 2
i, j, k = 0, 0, len(queue1)
while i < 2*k and j < 2*k and sum_q1 != sum_q2:
if sum_q1 < total:
sum_q1 += queue2[j]
sum_q2 -= queue2[j]
queue1.append(queue2[j])
j += 1
else:
sum_q2 += queue1[i]
sum_q1 -= queue1[i]
queue2.append(queue1[i])
i += 1
if sum_q1 == sum_q2:
return i + j
else:
return -1
< 풀이 과정 >
import heapq
def solution(N, road, K):
inf = 1e9
graph = [[] for _ in range(N+1)]
dist = [inf] * (N+1)
for direction in road:
a, b, c = direction
graph[a].append((b, c))
graph[b].append((a, c))
def dijkstra(start):
heap = []
dist[start] = 0
heapq.heappush(heap, (0, start))
while heap:
cost, node = heapq.heappop(heap)
if dist[node] < cost:
continue
for start_node in graph[node]:
node_plus = cost + start_node[1]
if node_plus < dist[start_node[0]]:
dist[start_node[0]] = node_plus
heapq.heappush(heap, (node_plus, start_node[0]))
dijkstra(1)
return len([i for i in dist if i <= K])
< 풀이 과정 >
다익스트라 - 최단 경로 탐색 알고리즘을 이용한 문제 풀이.
dist는 모두 무한대로 그려 놓은 후 경로마다 값을 입력하여 최단 경로 찾는 방법 선택