Skip to content

Commit 0c3ae2f

Browse files
committed
백준 1927번 최소 힙
문제 링크: https://www.acmicpc.net/problem/1927
1 parent 58ed80b commit 0c3ae2f

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

백준 1927번 최소 힙/main.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
from typing import List
3+
import heapq
4+
5+
6+
def solve():
7+
N: int = int(sys.stdin.readline().strip())
8+
heap: List[int] = []
9+
heapq.heapify(heap)
10+
11+
for _ in range(N):
12+
n: int = int(sys.stdin.readline().strip())
13+
14+
if n == 0:
15+
if len(heap) == 0:
16+
print(0)
17+
else:
18+
print(heapq.heappop(heap))
19+
else:
20+
heapq.heappush(heap, n)
21+
22+
23+
if __name__ == '__main__':
24+
solve()

백준 1927번 최소 힙/test1.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
9
2+
0
3+
12345678
4+
1
5+
2
6+
0
7+
0
8+
0
9+
0
10+
32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0
2+
1
3+
2
4+
12345678
5+
0
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')

0 commit comments

Comments
 (0)