mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-07 12:53:55 +08:00
* 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>
49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
"""
|
|
File: binary_search_edge.py
|
|
Created Time: 2023-08-04
|
|
Author: krahets (krahets@163.com)
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.append(str(Path(__file__).parent.parent))
|
|
from binary_search_insertion import binary_search_insertion
|
|
|
|
|
|
def binary_search_left_edge(nums: list[int], target: int) -> int:
|
|
"""最左端のターゲットの二分探索"""
|
|
# ターゲットの挿入位置を見つけることと同等
|
|
i = binary_search_insertion(nums, target)
|
|
# ターゲットが見つからなかった場合、-1 を返す
|
|
if i == len(nums) or nums[i] != target:
|
|
return -1
|
|
# ターゲットが見つかった場合、インデックス i を返す
|
|
return i
|
|
|
|
|
|
def binary_search_right_edge(nums: list[int], target: int) -> int:
|
|
"""最右端のターゲットの二分探索"""
|
|
# 最左端のターゲット + 1 を見つけることに変換
|
|
i = binary_search_insertion(nums, target + 1)
|
|
# j は最右端のターゲットを指し、i はターゲットより大きい最初の要素を指す
|
|
j = i - 1
|
|
# ターゲットが見つからなかった場合、-1 を返す
|
|
if j == -1 or nums[j] != target:
|
|
return -1
|
|
# ターゲットが見つかった場合、インデックス j を返す
|
|
return j
|
|
|
|
|
|
"""ドライバーコード"""
|
|
if __name__ == "__main__":
|
|
# 重複要素のある配列
|
|
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
|
|
print(f"\n配列 nums = {nums}")
|
|
|
|
# 左端と右端の境界の二分探索
|
|
for target in [6, 7]:
|
|
index = binary_search_left_edge(nums, target)
|
|
print(f"最左端の要素 {target} のインデックスは {index}")
|
|
index = binary_search_right_edge(nums, target)
|
|
print(f"最右端の要素 {target} のインデックスは {index}") |