Skip to content

035-search-insert-position#1

Open
05ryt31 wants to merge 1 commit intomainfrom
feat/035
Open

035-search-insert-position#1
05ryt31 wants to merge 1 commit intomainfrom
feat/035

Conversation

@05ryt31
Copy link
Copy Markdown
Owner

@05ryt31 05ryt31 commented Nov 29, 2025

初PRです。これからよろしくお願いいたします。

leetcodeチャンネルでBinary Searchから取り組むと良いとの投稿を見たので、この問題を選んでみました。

以前までNeetcodeをやっていて、アルゴリズムに関する知見はあるものの、問題はあまり解けず苦戦している状況です。
問題解決力を鍛えるアルゴリズムとデータ構造という本も以前に読みました。

問題リンク

https://leetcode.com/problems/search-insert-position/description/

問題文の概要

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with O(log n) runtime complexity.

次に解く予定の問題

153 find-minimum-in-rotated-sorted-array
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Nitpick comments (1)
035-search-insert-position/memo.md (1)

74-74: Add language specifier to code fence for better readability.

The code block would benefit from specifying python as the language for syntax highlighting.

Apply this change:

-```
+```python
 class Solution:
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a761a99 and 30867f2.

📒 Files selected for processing (4)
  • 035-search-insert-position/memo.md (1 hunks)
  • 035-search-insert-position/step1.py (1 hunks)
  • 035-search-insert-position/step2.py (1 hunks)
  • 035-search-insert-position/step3.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
035-search-insert-position/step2.py (2)
035-search-insert-position/step1.py (2)
  • Solution (3-6)
  • searchInsert (4-6)
035-search-insert-position/step3.py (2)
  • Solution (1-15)
  • searchInsert (2-15)
035-search-insert-position/step1.py (2)
035-search-insert-position/step2.py (2)
  • Solution (2-17)
  • searchInsert (3-17)
035-search-insert-position/step3.py (2)
  • Solution (1-15)
  • searchInsert (2-15)
🪛 LanguageTool
035-search-insert-position/memo.md

[grammar] ~32-~32: Ensure spelling is correct
Context: ...数字がTargetよりも大きければそのまま右側を走査、 小さければ左側を走査する そのまま見つかればIndexをreturn 見つからない場合、 ## Step2での気づき 解答解説を見た時、left, ...

(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)

🪛 markdownlint-cli2 (0.18.1)
035-search-insert-position/memo.md

74-74: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🪛 Ruff (0.14.6)
035-search-insert-position/step2.py

3-3: Undefined name List

(F821)

035-search-insert-position/step3.py

2-2: Undefined name List

(F821)

035-search-insert-position/step1.py

6-7: Expected an indented block after if statement

(invalid-syntax)

🔇 Additional comments (4)
035-search-insert-position/step3.py (2)

3-8: Binary search initialization looks good.

The pointer initialization and loop condition (left <= right) are correct for this binary search implementation.


11-15: Pointer updates and insertion logic are correct.

The adjustments right = mid - 1 and left = mid + 1 properly narrow the search space, and returning left as the insertion position is correct.

035-search-insert-position/step1.py (1)

1-4: Import and class definition are correct.

Good use of the typing.List import for type hints.

035-search-insert-position/step2.py (1)

4-6: Pointer initialization is correct.

The setup with left = 0 and right = len(nums) - 1 is appropriate for binary search.

Repository owner deleted a comment from coderabbitai bot Nov 29, 2025
Repository owner deleted a comment from coderabbitai bot Nov 29, 2025
Repository owner deleted a comment from coderabbitai bot Nov 29, 2025
Repository owner deleted a comment from coderabbitai bot Nov 29, 2025
Repository owner deleted a comment from coderabbitai bot Nov 29, 2025
Repository owner deleted a comment from coderabbitai bot Nov 29, 2025
見つからない場合のreturnするIndexの考え方も吸収できた。

### 間違えていたところとしては、
1. left <= rightとしていなかった。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

念のため質問させてください。なぜ left <= right としないと意図しない挙動になるのか説明してみていただけますか?また、

  • left より左側の要素にはどのような特徴があるか?
  • left の位置の要素にはどのような特徴があるか?
  • left < x < right の x の位置の要素にはどのような特徴があるか?
  • right の位置の要素にはどのような特徴があるか?
  • right より右側の要素にはどのような特徴があるか?

それぞれ答えてみていただけますか?

1. left <= rightとしていなかった。
2. right, leftの値更新の際の1ずらすところができていなかった。

right = mid - 1, left = mid + 1としないと、left < rightという制約を超えることがないため無限ループを引き起こしてしまう。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらについても念のため質問させてください。

  • なぜ right = mid となっているところ right = mid - 1 としても正しい答えが返って来ますか?
  • 同様に left = mid となっているところを left = mid + 1 としても正しい答えが返って来ますか?
  • right や left を動かし過ぎて、答えとなるインデックスを通り過ぎてしまうことはありませんか?


right = mid - 1, left = mid + 1としないと、left < rightという制約を超えることがないため無限ループを引き起こしてしまう。

## GPTからもらった解答
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://nuc.hatenadiary.org/entry/2025/11/29/ の「二分探索を読めるか」の項も参考にされることをおすすめします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちら読ませていただきました。大変面白い内容なので他の項も含めて今後参考にさせていただきます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants