Algorithm/Greedy27 [파이썬] 백준 1715 : 카드 정렬하기 (골드4) [파이썬] 백준 1715 : 카드 정렬하기 (골드4)https://www.acmicpc.net/problem/1715방향성 생각힙에서 가장 작은 원소 2개를 뽑아서 더한 후, 다시 힙에 넣어주면 된다.힙의 길이가 1이 될 때 까지 반복하기 풀이import heapq as hqimport sysinput = lambda : sys.stdin.readline().strip()heap = [int(input()) for _ in range(int(input()))]hq.heapify(heap)answer = 0while len(heap) > 1: temp = 0 for _ in range(2): temp += hq.heappop(heap) answer += temp hq.he.. 2025. 3. 30. [파이썬] 백준 19598 : 최소 회의실 개수 (골드5) [파이썬] 백준 19598 : 최소 회의실 개수 (골드5)https://www.acmicpc.net/problem/19598풀이방향성 생각1374랑 같은 문제 전체코드import heapq as hqimport sysinput = lambda : sys.stdin.readline().strip()N = int(input())arr = sorted(tuple(map(int,input().split())) for _ in range(N))answer = 1heap = [arr[0][1]]for s,e in arr[1:]: if heap[0] 일단 시작점을 힙에 넣고 시작그 다음 정보를 뽑아내면서, 현재 루트의 끝나는 시간인 heap[0]보다 크면 강의실 비우기 (heappop)해당 정보도 강의실 이용하.. 2025. 3. 29. [파이썬] 백준 1715 : 카드 정렬하기 (골드4) [파이썬] 백준 1715 : 카드 정렬하기 (골드4)https://www.acmicpc.net/problem/1715방향성 생각힙에서 가장 작은 원소 2개를 뽑아서 더한 후, 다시 힙에 넣어주면 된다.힙의 길이가 1이 될 때 까지 반복하기풀이import heapq as hqimport sysinput = lambda : sys.stdin.readline().strip()heap = [int(input()) for _ in range(int(input()))]hq.heapify(heap)answer = 0while len(heap) > 1: temp = 0 for _ in range(2): temp += hq.heappop(heap) answer += temp hq.hea.. 2025. 3. 29. [파이썬] 백준 1374 : 강의실 (골드5) [파이썬] 백준 1374 : 강의실 (골드5)https://www.acmicpc.net/problem/1374풀이방향성 생각시작시간 가장 빠른거 부터 뽑기시작 시간이 같으면, 빨리 끝나는 강의일수록 다음 강의실을 안 쓸 수 있다. 전체코드import heapq as hqimport sysinput = lambda : sys.stdin.readline().strip()N = int(input())arr = [tuple(map(int,input().split())) for _ in range(N)]arr.sort(key = lambda x : (x[1],x[2]))heap = []hq.heappush(heap,arr[0][2])answer = 1count = 1for i in range(1,N): _,.. 2025. 3. 28. 이전 1 2 3 4 ··· 7 다음