mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
committed by
GitHub
parent
2487a27036
commit
954c45864b
71
ja/codes/python/chapter_heap/heap.py
Normal file
71
ja/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モジュールはデフォルトでMinHeapを実装
|
||||
# ヒープに入れる前に「要素を反転」することを考慮し、比較演算子を逆転させて最大ヒープを実装
|
||||
# この例では、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
ja/codes/python/chapter_heap/my_heap.py
Normal file
137
ja/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
|
||||
# 2つのノードを交換
|
||||
self.swap(i, p)
|
||||
# 上向きのループヒープ化
|
||||
i = p
|
||||
|
||||
def pop(self) -> int:
|
||||
"""要素をヒープから出す"""
|
||||
# 空の処理
|
||||
if self.is_empty():
|
||||
raise IndexError("Heap is empty")
|
||||
# ルートノードと最右端の葉ノードを交換(最初の要素と最後の要素を交換)
|
||||
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
|
||||
# 2つのノードを交換
|
||||
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
ja/codes/python/chapter_heap/top_k.py
Normal file
39
ja/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