mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-27 23:11:57 +08:00
feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
This commit is contained in:
71
ko/codes/python/chapter_heap/heap.py
Normal file
71
ko/codes/python/chapter_heap/heap.py
Normal file
@@ -0,0 +1,71 @@
|
||||
"""
|
||||
File: heap.py
|
||||
Created Time: 2023-02-23
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.append(str(Path(__file__).parent.parent))
|
||||
from modules import print_heap
|
||||
|
||||
import heapq
|
||||
|
||||
|
||||
def test_push(heap: list, val: int, flag: int = 1):
|
||||
heapq.heappush(heap, flag * val) # 힙에 원소 삽입
|
||||
print(f"\n원소를 힙에 삽입한 후(원소: {val})")
|
||||
print_heap([flag * val for val in heap])
|
||||
|
||||
|
||||
def test_pop(heap: list, flag: int = 1):
|
||||
val = flag * heapq.heappop(heap) # 힙의 루트 원소 꺼내기
|
||||
print(f"\n힙의 루트 원소를 꺼낸 후(원소: {val})")
|
||||
print_heap([flag * val for val in heap])
|
||||
|
||||
|
||||
"""실행 코드"""
|
||||
if __name__ == "__main__":
|
||||
# 최소 힙 초기화
|
||||
min_heap, flag = [], 1
|
||||
# 최대 힙 초기화
|
||||
max_heap, flag = [], -1
|
||||
|
||||
print("\n다음 테스트 예제는 최대 힙이다")
|
||||
# Python의 heapq 모듈은 기본적으로 최소 힙을 구현한다
|
||||
# 원소의 부호를 바꾸어 힙에 삽입하면 대소 관계가 뒤집히므로 최대 힙을 구현할 수 있다
|
||||
# 이 예제에서 flag = 1은 최소 힙에, flag = -1은 최대 힙에 해당한다
|
||||
|
||||
# 힙에 원소 삽입
|
||||
test_push(max_heap, 1, flag)
|
||||
test_push(max_heap, 3, flag)
|
||||
test_push(max_heap, 2, flag)
|
||||
test_push(max_heap, 5, flag)
|
||||
test_push(max_heap, 4, flag)
|
||||
|
||||
# 힙의 루트 원소 반환
|
||||
peek: int = flag * max_heap[0]
|
||||
print(f"\n힙의 루트 원소는 {peek}이다")
|
||||
|
||||
# 힙의 루트 원소 꺼내기
|
||||
test_pop(max_heap, flag)
|
||||
test_pop(max_heap, flag)
|
||||
test_pop(max_heap, flag)
|
||||
test_pop(max_heap, flag)
|
||||
test_pop(max_heap, flag)
|
||||
|
||||
# 힙 크기 반환
|
||||
size: int = len(max_heap)
|
||||
print(f"\n힙의 원소 개수는 {size}개이다")
|
||||
|
||||
# 힙이 비어 있는지 판단
|
||||
is_empty: bool = not max_heap
|
||||
print(f"\n힙이 비어 있는가 {is_empty}")
|
||||
|
||||
# 리스트를 입력하여 힙 구성
|
||||
# 시간 복잡도는 O(n)이며 O(nlogn)이 아니다
|
||||
min_heap = [1, 3, 2, 5, 4]
|
||||
heapq.heapify(min_heap)
|
||||
print("\n리스트를 입력하여 최소 힙을 구성한 후")
|
||||
print_heap(min_heap)
|
||||
137
ko/codes/python/chapter_heap/my_heap.py
Normal file
137
ko/codes/python/chapter_heap/my_heap.py
Normal file
@@ -0,0 +1,137 @@
|
||||
"""
|
||||
File: my_heap.py
|
||||
Created Time: 2023-02-23
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.append(str(Path(__file__).parent.parent))
|
||||
from modules import print_heap
|
||||
|
||||
|
||||
class MaxHeap:
|
||||
"""최대 힙"""
|
||||
|
||||
def __init__(self, nums: list[int]):
|
||||
"""생성자: 입력 리스트를 바탕으로 힙 구성"""
|
||||
# 리스트 원소를 그대로 힙에 추가한다
|
||||
self.max_heap = nums
|
||||
# 리프 노드를 제외한 모든 노드에 힙 재정렬을 수행한다
|
||||
for i in range(self.parent(self.size() - 1), -1, -1):
|
||||
self.sift_down(i)
|
||||
|
||||
def left(self, i: int) -> int:
|
||||
"""왼쪽 자식 노드의 인덱스 반환"""
|
||||
return 2 * i + 1
|
||||
|
||||
def right(self, i: int) -> int:
|
||||
"""오른쪽 자식 노드의 인덱스 반환"""
|
||||
return 2 * i + 2
|
||||
|
||||
def parent(self, i: int) -> int:
|
||||
"""부모 노드의 인덱스 반환"""
|
||||
return (i - 1) // 2 # 나눗셈의 결과를 내림한다
|
||||
|
||||
def swap(self, i: int, j: int):
|
||||
"""원소 교환"""
|
||||
self.max_heap[i], self.max_heap[j] = self.max_heap[j], self.max_heap[i]
|
||||
|
||||
def size(self) -> int:
|
||||
"""힙 크기 반환"""
|
||||
return len(self.max_heap)
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""힙이 비어 있는지 판단"""
|
||||
return self.size() == 0
|
||||
|
||||
def peek(self) -> int:
|
||||
"""힙의 루트 원소 접근"""
|
||||
return self.max_heap[0]
|
||||
|
||||
def push(self, val: int):
|
||||
"""힙에 원소 삽입"""
|
||||
# 노드 추가
|
||||
self.max_heap.append(val)
|
||||
# 아래에서 위로 힙을 재정렬한다
|
||||
self.sift_up(self.size() - 1)
|
||||
|
||||
def sift_up(self, i: int):
|
||||
"""노드 i에서 시작하여 아래에서 위로 힙 재정렬"""
|
||||
while True:
|
||||
# 노드 i의 부모 노드를 구한다
|
||||
p = self.parent(i)
|
||||
# 루트 노드를 넘어가거나 노드를 수정할 필요가 없으면 힙 재정렬을 마친다
|
||||
if p < 0 or self.max_heap[i] <= self.max_heap[p]:
|
||||
break
|
||||
# 두 노드를 교환한다
|
||||
self.swap(i, p)
|
||||
# 위쪽으로 힙 재정렬을 반복한다
|
||||
i = p
|
||||
|
||||
def pop(self) -> int:
|
||||
"""힙에서 원소 꺼내기"""
|
||||
# 비어 있는 경우를 처리한다
|
||||
if self.is_empty():
|
||||
raise IndexError("힙이 비어 있다")
|
||||
# 루트 노드와 가장 오른쪽 리프 노드를 교환한다(첫 원소와 마지막 원소 교환)
|
||||
self.swap(0, self.size() - 1)
|
||||
# 노드 삭제
|
||||
val = self.max_heap.pop()
|
||||
# 위에서 아래로 힙을 재정렬한다
|
||||
self.sift_down(0)
|
||||
# 힙의 루트 원소를 반환한다
|
||||
return val
|
||||
|
||||
def sift_down(self, i: int):
|
||||
"""노드 i에서 시작하여 위에서 아래로 힙 재정렬"""
|
||||
while True:
|
||||
# 노드 i, l, r 중 값이 가장 큰 노드를 찾아 ma로 표시한다
|
||||
l, r, ma = self.left(i), self.right(i), i
|
||||
if l < self.size() and self.max_heap[l] > self.max_heap[ma]:
|
||||
ma = l
|
||||
if r < self.size() and self.max_heap[r] > self.max_heap[ma]:
|
||||
ma = r
|
||||
# 노드 i의 값이 가장 크거나 인덱스 l, r이 범위를 벗어나면 힙을 더 재정렬할 필요가 없으므로 종료한다
|
||||
if ma == i:
|
||||
break
|
||||
# 두 노드를 교환한다
|
||||
self.swap(i, ma)
|
||||
# 아래쪽으로 힙 재정렬을 반복한다
|
||||
i = ma
|
||||
|
||||
def print(self):
|
||||
"""힙 출력(이진 트리)"""
|
||||
print_heap(self.max_heap)
|
||||
|
||||
|
||||
"""실행 코드"""
|
||||
if __name__ == "__main__":
|
||||
# 최대 힙 초기화
|
||||
max_heap = MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2])
|
||||
print("\n리스트를 입력하여 힙을 구성한 후")
|
||||
max_heap.print()
|
||||
|
||||
# 힙의 루트 원소 반환
|
||||
peek = max_heap.peek()
|
||||
print(f"\n힙의 루트 원소는 {peek}이다")
|
||||
|
||||
# 힙에 원소 삽입
|
||||
val = 7
|
||||
max_heap.push(val)
|
||||
print(f"\n원소를 힙에 삽입한 후(원소: {val})")
|
||||
max_heap.print()
|
||||
|
||||
# 힙의 루트 원소 꺼내기
|
||||
peek = max_heap.pop()
|
||||
print(f"\n힙의 루트 원소를 꺼낸 후(원소: {peek})")
|
||||
max_heap.print()
|
||||
|
||||
# 힙 크기 반환
|
||||
size = max_heap.size()
|
||||
print(f"\n힙의 원소 개수는 {size}개이다")
|
||||
|
||||
# 힙이 비어 있는지 판단
|
||||
is_empty = max_heap.is_empty()
|
||||
print(f"\n힙이 비어 있는가 {is_empty}")
|
||||
39
ko/codes/python/chapter_heap/top_k.py
Normal file
39
ko/codes/python/chapter_heap/top_k.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""
|
||||
File: top_k.py
|
||||
Created Time: 2023-06-10
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.append(str(Path(__file__).parent.parent))
|
||||
from modules import print_heap
|
||||
|
||||
import heapq
|
||||
|
||||
|
||||
def top_k_heap(nums: list[int], k: int) -> list[int]:
|
||||
"""힙을 이용하여 배열에서 가장 큰 k개 원소 탐색"""
|
||||
# 최소 힙 초기화
|
||||
heap = []
|
||||
# 배열의 앞쪽 k개 원소를 힙에 삽입한다
|
||||
for i in range(k):
|
||||
heapq.heappush(heap, nums[i])
|
||||
# k+1번째 원소부터 힙의 길이를 k로 유지한다
|
||||
for i in range(k, len(nums)):
|
||||
# 현재 원소가 힙의 루트 원소보다 크면 루트 원소를 꺼내고 현재 원소를 힙에 삽입한다
|
||||
if nums[i] > heap[0]:
|
||||
heapq.heappop(heap)
|
||||
heapq.heappush(heap, nums[i])
|
||||
return heap
|
||||
|
||||
|
||||
"""실행 코드"""
|
||||
if __name__ == "__main__":
|
||||
nums = [1, 7, 6, 3, 2]
|
||||
k = 3
|
||||
|
||||
res = top_k_heap(nums, k)
|
||||
print(f"가장 큰 {k}개 원소")
|
||||
print_heap(res)
|
||||
Reference in New Issue
Block a user