Skip to content

Commit 2f5375d

Browse files
committed
docs(leetcode/69): update README and implementation notes for Claude 4.6 extended
1 parent 705e474 commit 2f5375d

5 files changed

Lines changed: 117 additions & 85 deletions

File tree

Algorithm/BinarySearch/leetcode/69. Sqrt(x)/Claude4.6 extended/README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@
44

55
## 目次
66

7-
- [概要](#overview)
8-
- [アルゴリズム要点 TL;DR](#tldr)
9-
- [図解](#figures)
10-
- [正しさのスケッチ](#correctness)
11-
- [計算量](#complexity)
12-
- [Python 実装](#impl)
13-
- [CPython 最適化ポイント](#cpython)
14-
- [エッジケースと検証観点](#edgecases)
15-
- [FAQ](#faq)
7+
- [Overview (概要)](#overview)
8+
- [Algorithm (アルゴリズム)](#algorithm)
9+
- [Complexity (計算量)](#complexity)
10+
- [Implementation (実装)](#implementation)
11+
- [Optimization (最適化)](#optimization)
1612

1713
---
1814

19-
<h2 id="overview">概要</h2>
15+
<h2 id="overview">Overview (概要)</h2>
2016

2117
### 問題要約
2218

@@ -64,7 +60,7 @@ class Solution:
6460

6561
---
6662

67-
<h2 id="figures">図解</h2>
63+
### 図解
6864

6965
### フローチャート
7066

@@ -141,15 +137,15 @@ _左から右へ: バリデーション → 早期リターン判定 → 二分
141137

142138
---
143139

144-
<h2 id="correctness">正しさのスケッチ</h2>
140+
### 正しさのスケッチ
145141

146142
### ループ不変条件(Loop Invariant)
147143

148144
ループの各反復前に以下が成立する:
149145

150146
> `(low - 1)^2 ≤ x` かつ `(high + 1)^2 > x`
151147
152-
つまり**真の答え `floor(√x)` は常に `[low, high]` の閉区間内に存在する**
148+
この不変条件により**真の答え `floor(√x)` は常に `[low - 1, high]` の範囲に含まれる**ことが保証される。ループ終了時 (`low > high`) には `high = floor(√x)` が確定する
153149

154150
| フェーズ | 不変条件の確認 |
155151
| -------------- | -------------------------------------------------------------------------- |
@@ -166,7 +162,7 @@ _左から右へ: バリデーション → 早期リターン判定 → 二分
166162

167163
---
168164

169-
<h2 id="complexity">計算量</h2>
165+
<h2 id="complexity">Complexity (計算量)</h2>
170166

171167
| 観点 | 計算量 | 補足 |
172168
| -------------- | -------- | --------------------------------------------- |
@@ -186,7 +182,9 @@ _左から右へ: バリデーション → 早期リターン判定 → 二分
186182
187183
---
188184

189-
<h2 id="impl">Python 実装</h2>
185+
<h2 id="implementation">Implementation (実装)</h2>
186+
187+
### Python 実装
190188

191189
```python
192190
from __future__ import annotations

Algorithm/BinarySearch/leetcode/69. Sqrt(x)/Claude4.6 extended/README_react.html

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
2525
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
2626
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-typescript.min.js"></script>
27+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-rust.min.js"></script>
2728
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.js"></script>
2829
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/toolbar/prism-toolbar.min.js"></script>
2930
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script>
@@ -350,9 +351,9 @@ <h3 class="outfit font-bold text-slate-800 text-base mb-3">入出力例</h3>
350351
// エッジケース: x=0, x=1 は即リターン
351352
if (x < 2) return x;
352353

353-
// 探索範囲: [1, x >> 1]
354+
// 探索範囲: [1, x >>> 1]
354355
let low: number = 1;
355-
let high: number = x >> 1; // x / 2(ビットシフト整数除算
356+
let high: number = x >>> 1; // x / 2(符号なし右シフト整数除算
356357

357358
// Loop Invariant:
358359
// low - 1 の二乗は x 以下
@@ -964,40 +965,55 @@ <h3 class="outfit font-bold text-indigo-800 text-base mb-3">
964965

965966
function TraceTable({ x }) {
966967
const rows = [];
967-
let low = 1,
968-
high = x >> 1;
969-
let iter = 0;
970-
while (low <= high && iter < 20) {
971-
iter++;
972-
const mid = (low + high) >> 1;
973-
const sq = mid * mid;
974-
let action = '';
975-
if (sq === x) {
976-
rows.push({ iter, low, high, mid, sq, action: 'return mid ✓' });
977-
break;
978-
} else if (sq < x) {
979-
action = `low = ${mid + 1}`;
980-
rows.push({ iter, low, high, mid, sq, action });
981-
low = mid + 1;
982-
} else {
983-
action = `high = ${mid - 1}`;
984-
rows.push({ iter, low, high, mid, sq, action });
985-
high = mid - 1;
986-
}
987-
}
988-
if (
989-
rows.length === 0 ||
990-
rows[rows.length - 1].action.startsWith('low') ||
991-
rows[rows.length - 1].action.startsWith('high')
992-
) {
968+
969+
// 早期リターンケース: x < 2
970+
if (x < 2) {
993971
rows.push({
994-
iter: iter + 1,
995-
low,
996-
high,
972+
iter: 0,
973+
low: '-',
974+
high: '-',
997975
mid: '-',
998976
sq: '-',
999-
action: `return high=${high} ✓`,
977+
action: `x < 2 → return ${x} ✓`,
1000978
});
979+
} else {
980+
let low = 1,
981+
high = x >>> 1;
982+
let iter = 0;
983+
984+
// 二分探索ループ: 実装と同じ終了条件 (while low <= high)
985+
while (low <= high) {
986+
iter++;
987+
const mid = (low + high) >>> 1;
988+
const sq = mid * mid;
989+
let action = '';
990+
991+
if (sq === x) {
992+
rows.push({ iter, low, high, mid, sq, action: `return mid=${mid} ✓` });
993+
break;
994+
} else if (sq < x) {
995+
action = `low = ${mid + 1}`;
996+
rows.push({ iter, low, high, mid, sq, action });
997+
low = mid + 1;
998+
} else {
999+
action = `high = ${mid - 1}`;
1000+
rows.push({ iter, low, high, mid, sq, action });
1001+
high = mid - 1;
1002+
}
1003+
}
1004+
1005+
// ループ終了後: high = floor(√x)
1006+
// 完全平方数でない場合のみ最終行を追加
1007+
if (rows.length === 0 || !rows[rows.length - 1].action.includes('return mid')) {
1008+
rows.push({
1009+
iter: iter + 1,
1010+
low,
1011+
high,
1012+
mid: '-',
1013+
sq: '-',
1014+
action: `return high=${high} ✓`,
1015+
});
1016+
}
10011017
}
10021018

10031019
return (

Algorithm/BinarySearch/leetcode/69. Sqrt(x)/Claude4.6 extended/Sqrt(x)_rust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## 競技プログラミング視点での分析
44

55
- 探索空間 `[0, x]` は単調増加 → **二分探索**が最適(最大31回のイテレーション)
6-
- `u32::MAX = 2³¹ - 1 = 2147483647``mid * mid` は最大 `u64``~4.6 × 10¹⁸` に達するため、**`u64` へのキャスト**でオーバーフローを防止
6+
- `u32::MAX = 2³¹ - 1 = 2147483647``mid` の最大値は `x >> 1` で約 `2³⁰`、よって `mid * mid` は最大 `(2³⁰)² ≈ 1.15×10¹⁸` に達するため、**`u64` へのキャスト**でオーバーフローを防止
77
- スタックのみ使用・ヒープアロケーション完全ゼロ → キャッシュに最適
88
- 全変数が `Copy` 型(`u32`, `u64`) → 借用・所有権の複雑性なし
99

Algorithm/BinarySearch/leetcode/69. Sqrt(x)/Claude4.6 extended/Sqrt(x)_typescript.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ function mySqrt(x: number): number {
8686
let high: number = x >> 1; // == Math.floor(x / 2), ビットシフトで整数除算
8787

8888
// Loop Invariant:
89-
// low の時点では low * low <= x が未確認
90-
// high の時点では high * high >= x が未確認
91-
//最終的に low > high になった時点で high = floor(√x)
89+
// (low - 1) * (low - 1) <= x (lowより小さい値の二乗はx以下)
90+
// (high + 1) * (high + 1) > x (highより大きい値の二乗はxより大きい)
91+
//この不変条件により、ループ終了時に high = floor(√x) が保証される
9292
while (low <= high) {
9393
// オーバーフロー防止: (low + high) >>> 1
9494
const mid: number = (low + high) >>> 1;
@@ -107,8 +107,10 @@ function mySqrt(x: number): number {
107107
}
108108

109109
// ループ終了後、high = floor(√x)
110-
// 例: x=8 → mid=3(9>8) → high=2, mid=1(1<8) → low=2
111-
// low=high=2 → mid=2(4<8) → low=3 → 終了: high=2 ✓
110+
// 例: x=8 → 初期 low=1, high=4
111+
// Iter1: mid=2, sq=4<8 → low=3
112+
// Iter2: mid=3, sq=9>8 → high=2
113+
// 終了: low(3) > high(2) → return high=2 ✓
112114
return high;
113115
}
114116
```

public/Algorithm/BinarySearch/leetcode/69. Sqrt(x)/Claude4.6 extended/README_react.html

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
2525
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
2626
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-typescript.min.js"></script>
27+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-rust.min.js"></script>
2728
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.js"></script>
2829
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/toolbar/prism-toolbar.min.js"></script>
2930
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script>
@@ -350,9 +351,9 @@ <h3 class="outfit font-bold text-slate-800 text-base mb-3">入出力例</h3>
350351
// エッジケース: x=0, x=1 は即リターン
351352
if (x < 2) return x;
352353

353-
// 探索範囲: [1, x >> 1]
354+
// 探索範囲: [1, x >>> 1]
354355
let low: number = 1;
355-
let high: number = x >> 1; // x / 2(ビットシフト整数除算
356+
let high: number = x >>> 1; // x / 2(符号なし右シフト整数除算
356357

357358
// Loop Invariant:
358359
// low - 1 の二乗は x 以下
@@ -964,40 +965,55 @@ <h3 class="outfit font-bold text-indigo-800 text-base mb-3">
964965

965966
function TraceTable({ x }) {
966967
const rows = [];
967-
let low = 1,
968-
high = x >> 1;
969-
let iter = 0;
970-
while (low <= high && iter < 20) {
971-
iter++;
972-
const mid = (low + high) >> 1;
973-
const sq = mid * mid;
974-
let action = '';
975-
if (sq === x) {
976-
rows.push({ iter, low, high, mid, sq, action: 'return mid ✓' });
977-
break;
978-
} else if (sq < x) {
979-
action = `low = ${mid + 1}`;
980-
rows.push({ iter, low, high, mid, sq, action });
981-
low = mid + 1;
982-
} else {
983-
action = `high = ${mid - 1}`;
984-
rows.push({ iter, low, high, mid, sq, action });
985-
high = mid - 1;
986-
}
987-
}
988-
if (
989-
rows.length === 0 ||
990-
rows[rows.length - 1].action.startsWith('low') ||
991-
rows[rows.length - 1].action.startsWith('high')
992-
) {
968+
969+
// 早期リターンケース: x < 2
970+
if (x < 2) {
993971
rows.push({
994-
iter: iter + 1,
995-
low,
996-
high,
972+
iter: 0,
973+
low: '-',
974+
high: '-',
997975
mid: '-',
998976
sq: '-',
999-
action: `return high=${high} ✓`,
977+
action: `x < 2 → return ${x} ✓`,
1000978
});
979+
} else {
980+
let low = 1,
981+
high = x >>> 1;
982+
let iter = 0;
983+
984+
// 二分探索ループ: 実装と同じ終了条件 (while low <= high)
985+
while (low <= high) {
986+
iter++;
987+
const mid = (low + high) >>> 1;
988+
const sq = mid * mid;
989+
let action = '';
990+
991+
if (sq === x) {
992+
rows.push({ iter, low, high, mid, sq, action: `return mid=${mid} ✓` });
993+
break;
994+
} else if (sq < x) {
995+
action = `low = ${mid + 1}`;
996+
rows.push({ iter, low, high, mid, sq, action });
997+
low = mid + 1;
998+
} else {
999+
action = `high = ${mid - 1}`;
1000+
rows.push({ iter, low, high, mid, sq, action });
1001+
high = mid - 1;
1002+
}
1003+
}
1004+
1005+
// ループ終了後: high = floor(√x)
1006+
// 完全平方数でない場合のみ最終行を追加
1007+
if (rows.length === 0 || !rows[rows.length - 1].action.includes('return mid')) {
1008+
rows.push({
1009+
iter: iter + 1,
1010+
low,
1011+
high,
1012+
mid: '-',
1013+
sq: '-',
1014+
action: `return high=${high} ✓`,
1015+
});
1016+
}
10011017
}
10021018

10031019
return (

0 commit comments

Comments
 (0)