diff --git "a/jaykxo/week06/11279_\354\265\234\353\214\200_\355\236\231.py" "b/jaykxo/week06/11279_\354\265\234\353\214\200_\355\236\231.py" new file mode 100644 index 0000000..49f7336 --- /dev/null +++ "b/jaykxo/week06/11279_\354\265\234\353\214\200_\355\236\231.py" @@ -0,0 +1,29 @@ +# 11279 최대 힙 +# PyPy3, 메모리: 114200 KB, 시간: 148 ms +# Python3, 메모리: 41172 KB, 시간: 96 ms + +# 최대 힙: 가장 큰 값이 항상 루트에 위치하도록 음수로 변환하여 저장 + +import heapq +import sys +input = sys.stdin.readline + +N = int(input()) +hq = [] +result = [] + +for _ in range(N): + x = int(input()) + + # 1927 최소힙과 달리, 음수도 입력 가능하므로 0보다 클 때만 삽입 + if x > 0: + heapq.heappush(hq, -x) # 최대 힙 구현 (음수로 변환하여 저장) + + # 0일 때만 출력 명령 수행 (음수는 데이터로 간주) + elif x == 0: + if not hq: # 힙이 비었으면 0 출력 + result.append(0) + else: + result.append(-heapq.heappop(hq)) # 힙에서 최대값 꺼내서 부호 복원 + +print('\n'.join(map(str, result))) # 결과 한 번에 출력 \ No newline at end of file diff --git "a/jaykxo/week06/11286_\354\240\210\353\214\223\352\260\222_\355\236\231.py" "b/jaykxo/week06/11286_\354\240\210\353\214\223\352\260\222_\355\236\231.py" new file mode 100644 index 0000000..d6d5858 --- /dev/null +++ "b/jaykxo/week06/11286_\354\240\210\353\214\223\352\260\222_\355\236\231.py" @@ -0,0 +1,26 @@ +# 11286 절댒값 힙 +# PyPy3, 메모리: 116424 KB, 시간: 168 ms +# Python3, 메모리: 43168 KB, 시간: 120 ms + +# 절댓값 힙: 절댓값이 가장 작은 값이 먼저 나오며, 같을 경우 실제 값이 작은 수(음수)가 우선 + +import heapq +import sys +input = sys.stdin.readline + +N = int(input()) +hq = [] +result = [] + +for _ in range(N): + x = int(input()) + + if x != 0: + heapq.heappush(hq, (abs(x), x)) # (절댓값, 실제값) 튜플형태로 저장 → 절댓값 기준, 동률 시 실제값 기준 정렬 + else: + if not hq: # 힙이 비었으면 0 출력 + result.append(0) + else: + result.append(heapq.heappop(hq)[1]) # 힙에서 (abs, 값) 튜플을 꺼내 실제값 출력 + +print('\n'.join(map(str, result))) \ No newline at end of file diff --git "a/jaykxo/week06/1927_\354\265\234\354\206\214_\355\236\231.py" "b/jaykxo/week06/1927_\354\265\234\354\206\214_\355\236\231.py" new file mode 100644 index 0000000..fa42d88 --- /dev/null +++ "b/jaykxo/week06/1927_\354\265\234\354\206\214_\355\236\231.py" @@ -0,0 +1,27 @@ +# 2075 최소 힙 +# PyPy3, 메모리: 114204 KB, 시간: 148 ms +# Python3, 메모리: 41172 KB, 시간: 92 ms + +# 최소 힙: 가장 작은 값이 항상 루트에 위치하도록 자동 정렬됨 + +import heapq +import sys +input = sys.stdin.readline + +N = int(input()) +hq = [] +result = [] + +for _ in range(N): + x = int(input()) + + if x != 0: # 0이 아니면 힙에 추가 + heapq.heappush(hq, x) # 힙에 값 추가 (자동으로 최소값이 루트로 정렬) + + else: # 0이면 최소값 출력 (없으면 0) + if not hq: + result.append(0) + else: + result.append(heapq.heappop(hq)) # 힙에서 최소값 꺼냄 + +print('\n'.join(map(str, result))) \ No newline at end of file diff --git "a/jaykxo/week06/2075_N\353\262\210\354\247\270_\355\201\260_\354\210\230.py" "b/jaykxo/week06/2075_N\353\262\210\354\247\270_\355\201\260_\354\210\230.py" new file mode 100644 index 0000000..4297f5d --- /dev/null +++ "b/jaykxo/week06/2075_N\353\262\210\354\247\270_\355\201\260_\354\210\230.py" @@ -0,0 +1,38 @@ +# 2075 N번째 큰 수 +# PyPy3, 메모리: 134040 KB, 시간: 444 ms +# Python3, 메모리 초과 ... + +import heapq +import sys +input = sys.stdin.readline + +N = int(input()) +hq = [] + +# N×N 크기의 표 입력: 행렬 형태로 저장 +board = [list(map(int, input().split())) for _ in range(N)] + +# 각 열의 맨 아래 원소(해당 열의 최댓값)를 초기 후보로 힙에 삽입 +for c in range(N): + + # (-값, 행, 열) 형태로 저장 → 최대값부터 pop할 수 있게 구성 + heapq.heappush(hq, (-board[N-1][c], N-1, c)) + +answer = 0 + +# 힙에서 가장 큰 값부터 N번 꺼내며 N번째로 큰 수를 찾음 +for _ in range(N): + + # 힙에서는 가장 작은 음수지만, 실제로는 가장 큰 값 pop + neg, r, c = heapq.heappop(hq) + + # 음수를 다시 양수로 변환하여 실제 값 저장 + answer = -neg + + # 같은 열에서 바로 위에 있는 값이 남아 있다면 + if r - 1 >= 0: + + # 해당 열의 위쪽 원소를 다음 후보로 힙에 추가 + heapq.heappush(hq, (-board[r-1][c], r-1, c)) + +print(answer) \ No newline at end of file