ํฐ์คํ ๋ฆฌ ๋ทฐ
algorithm/baekjoon
[๋ฐฑ์ค] 1927. ์ต์ํ / 11279. ์ต๋ํ / python ํ์ด์ฌ
jen jen 2021. 6. 6. 00:01728x90
๐ฉ heap, ์๋ฃ๊ตฌ์กฐ, ์ฐ์ ์์ ํ
thinking
input์ด 10๋ง์ค ์ด์์ผ๋ฏ๋ก sys.stdin.readline()
์ ์ฌ์ฉํ๊ณ ,
ํ์ด์ฌ์ heapq
๋ชจ๋์ ์ฌ์ฉํ์ฌ ํด๊ฒฐํ๋ค.
์ฝ๋
1927. ์ต์ํ
import sys
import heapq
N = int(sys.stdin.readline())
heap = []
for _ in range(N):
x = int(sys.stdin.readline())
if x == 0:
if heap:
print(heapq.heappop(heap))
else:
print('0')
else:
heapq.heappush(heap, x)
11279. ์ต๋ํ
heapq ๋ชจ๋์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ต์ํ๋ง์ ์ง์ํ๊ธฐ ๋๋ฌธ์ ์ต๋ํ์ ๊ตฌํ๋ ค๋ฉด ํํ์ด๋ ๋ฆฌ์คํธํ์์ผ๋ก ๋ฃ์ด 0๋ฒ์งธ ์ธ๋ฑ์ค์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ต์ํ์ ๊ตฌ์ฑํด์ผํ๋ค.
import sys
import heapq
N = int(sys.stdin.readline())
heap = []
for _ in range(N):
x = int(sys.stdin.readline())
if x == 0:
if heap:
print(heapq.heappop(heap)[1])
else:
print('0')
else:
heapq.heappush(heap, [-x,x])
'algorithm > baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 2579. ๊ณ๋จ์ค๋ฅด๊ธฐ / python ํ์ด์ฌ (0) | 2021.06.30 |
---|---|
[๋ฐฑ์ค] 2003. ์๋ค์ ํฉ 2 / python ํ์ด์ฌ / ํฌํฌ์ธํฐ, ๊ตฌ๊ฐํฉ (0) | 2021.06.30 |
[๋ฐฑ์ค] 10026. ์ ๋ก์์ฝ / python ํ์ด์ฌ (0) | 2021.06.03 |
[๋ฐฑ์ค] 1107. ๋ฆฌ๋ชจ์ปจ / python ํ์ด์ฌ (0) | 2021.06.03 |
[๋ฐฑ์ค] 7576. ํ ๋งํ / python ํ์ด์ฌ (0) | 2021.06.02 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
TAG
- BFS
- ์ผ์ฑ๊ธฐ์ถ
- react
- 17406 ๋ฐฐ์ด๋๋ฆฌ๊ธฐ4
- ๋ธ๋ฃจํธํฌ์ค
- Python
- 20056 ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ
- ์์ด๋๋ง์๊ธฐ
- ๋ณด์์ผํ
- ํ์ด์ฌ
- ๋ฐฑ์ค
- git ๋ฏธ๋ฌ๋ง
- merge ์๋ฌ
- swea
- ์ผ์ฑ์ฝํ
- ์๊ณ ๋ฆฌ์ฆ
- dp
- 20057 ๋ง๋ฒ์ฌ ์์ด์ ํ ๋ค์ด๋
- ๊ธฐ์ง๊ตญ์ค์น
- ํ๋ก๊ทธ๋๋จธ์ค
- 21609 ์์ด ์คํ๊ต
- 2579 ๊ณ๋จ์ค๋ฅด๊ธฐ
- dfs
- merge์๋ฌ
- 2018 ์นด์นด์ค ๊ณต์ฑ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
- Total
- Today
- Yesterday
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ