알고리즘 4

[백준][자료구조/힙] 1655.가운데를 말해요 (파이썬/Python)

import sys import heapq input = sys.stdin.readline n=int(input()) leftHeap=[] rightHeap=[] answer=[] for i in range(n): num=int(input()) if len(leftHeap)==len(rightHeap): heapq.heappush(leftHeap, (-num, num)) else: heapq.heappush(rightHeap, (num, num)) if rightHeap and leftHeap[0][1] > rightHeap[0][0]: min=heapq.heappop(rightHeap)[0] max=heapq.heappop(leftHeap)[1] heapq.heappush(leftHeap, (-min,..

[프로그래머스][Lv2] - 다리를 지나는 트럭(파이썬/Python)

from collections import deque def solution(bridge_length, weight, truck_weights): time = 0 b = deque([0 for _ in range(bridge_length)]) w = 0 #다리 위의 무게 q = deque(truck_weights) while len(q) > 0 or w > 0: #지워주는 작업부터 시작. # 0으로 초기화 해주어서 popleft의 문제가 생기지 않는 것. removeTruck = b.popleft() w -= removeTruck if len(q) and q[0] + w

Python - 반복하는 알고리즘

1부터 n까지 정수의 합 구하기 print("1부터 n까지 정수의 합을 구합니다.") n = int(input()) sum = 0 i = 1 while i b : a, b = b, a #a가 b보다 크다면 a에 b값을 b엔 a값을 넣어준다. sum = 0 for i in range(a,b+1): sum += i print(f"{a}부터 {b}까지의 정수의 합은 {sum}입니다.") a와 b를 오름차순으로 정렬한 뒤 다음 해당 범위의 모든 정수를 더하는 프로그램 a, b = b, a #a와 b의 값을 교환(단일 대입문일 때 사용가능) 두 값 교환하기 1 a, b = b, a 단일 대입문의 교환 과정. 우변의 b, a에 의해 두 값을 압축한 튜플(b, a)가 생성 대입할 때 튜플(b, a)를 다시 풀어 b,..