ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90

๐Ÿšฉ 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])
๋Œ“๊ธ€