From 8afec2d6e0d0934fa60ea8bcb018ca1308a750b6 Mon Sep 17 00:00:00 2001 From: nittoco <166355467+nittoco@users.noreply.github.com> Date: Tue, 16 Jul 2024 22:38:03 +0900 Subject: [PATCH 1/2] 35. Search Insert Position.md 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. --- 35. Search Insert Position.md | 74 +++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 35. Search Insert Position.md diff --git a/35. Search Insert Position.md b/35. Search Insert Position.md new file mode 100644 index 0000000..3d1d00b --- /dev/null +++ b/35. Search Insert Position.md @@ -0,0 +1,74 @@ +### Step1 + +- smallerの設定を0にしてしまった。最初の要素がtargetより大きかったら意味ない +- larger_or_equalを返すかsmallerを返すかでindexの計算がこんがらがり頭を使った、苦労した +- smallerに-1を使うのがお行儀が悪いのかはよくわからない???(Atcoderとかのコードだとよくあるけどやばいのかも) + +```python + +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + larger_or_equal = len(nums) + smaller = -1 + while larger_or_equal - smaller > 1: + mid = (larger_or_equal + smaller) // 2 + if nums[mid] >= target: + larger_or_equal = mid + else: + smaller = mid + return larger_or_equal +``` + +## Step2 + +- 二分探索について。しっくりできていない。[この辺の議論](https://discord.com/channels/1084280443945353267/1192736784354918470/1199018938005213234)を拝見。 + - 区間で考えるというアイデアがなかった + - 理解に時間がかかったが 閉開区間の場合 + - 最後の状態は、「見ている区間がない」状態にする←これを誤解してた + - 閉開区間だったら、left = rightになればOK + - 今見ているもの(mid)は、必ず区間に含まないものとしてどちらかに寄せる + - そうしないと、例えばleft = midの時区間が永遠に減らないかも + - 今回はinsert_leftなので、targetとnums[mid]がイコールであるならばrightを動かして、みている区間を左に寄せる + - left ≤ mid < rightなので + - leftを動かすとき、必ず区間を減らすにはleft = mid + 1 + - rightを動かす時、right = midにしても区間は必ず1減る + - mid = (left + right)//2 とすればleft ≤ mid < rightは満たす +- この辺も拝見 + - bisectのコード https://github.com/python/cpython/blob/3.12/Lib/bisect.py + - 上の議論と一緒のコードでした + - elifよりelseが良い + - https://github.com/SuperHotDogCat/coding-interview/pull/9/files +- if文、nums[mid] < targetという向きの方がtarget > nums[mid]よりスッキリするかも + - whileの中もright > leftがいいかも + +```python + +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left = 0 + right = len(nums) + while right > left: + mid = (left + right) // 2 + if nums[mid] < target: + left = mid + 1 + else: + right = mid + return left +``` + +## Step3 + +```python + +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left = 0 + right = len(nums) + while right > left: + mid = (left + right) // 2 + if nums[mid] < target: + left = mid + 1 + else: + right = mid + return left +``` From 2bf4abc1bbeedcb6300fb9bd26e131c84f1c97cd Mon Sep 17 00:00:00 2001 From: nittoco <166355467+nittoco@users.noreply.github.com> Date: Sat, 20 Jul 2024 12:44:41 +0900 Subject: [PATCH 2/2] Update 35. Search Insert Position.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steo4を追加 --- 35. Search Insert Position.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/35. Search Insert Position.md b/35. Search Insert Position.md index 3d1d00b..5bb3a54 100644 --- a/35. Search Insert Position.md +++ b/35. Search Insert Position.md @@ -72,3 +72,19 @@ class Solution: right = mid return left ``` + +## Step4 +```python + +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left = 0 + right = len(nums) + while left < right: + mid = (left + right) // 2 + if nums[mid] < target: + left = mid + 1 + else: + right = mid + return left +```