From 1bfaf0f3893538173ccdd2a0a1130e81fa5f6a64 Mon Sep 17 00:00:00 2001 From: Ayato Hayashi Date: Tue, 5 Mar 2024 20:50:59 +0900 Subject: [PATCH 1/5] Create 35. Search Insert Position.md --- 35. Search Insert Position.md | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 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..618fc44 --- /dev/null +++ b/35. Search Insert Position.md @@ -0,0 +1,58 @@ +二分探索。 + +1st + +target以上の値になる位置を求める。それがinsert position。 +pythonではオーバーフローを考慮しなくて良いが、考慮する必要がある言語だと mid = left + (right - left) // 2のようにする。 +```python +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left, right = 0, len(nums) + while left < right: + mid = (left + right) // 2 + if nums[mid] >= target: + right = mid + else: + left = mid + 1 + return left +``` + +2nd +bisectモジュール。 +bisect_leftの実装はここ。https://github.com/python/cpython/blob/cfbdce72083fca791947cbb18114115c90738d99/Lib/bisect.py#L74 +Pythonで実装されていて特に変なことはしてない。 +```python +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + return bisect.bisect_left(nums, target) +``` + +`[:i]`についてtargetより小さく、`[i:]`についてtarget以上になるiを求める。 +```python +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + low, high = 0, len(nums) + while low < high: + mid = (low + high) // 2 + if nums[mid] < target: + low = mid + 1 + else: + high = mid + return low +``` + +3rd + +```python +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left, right = 0, len(nums) + while left < right: + mid = (left + right) // 2 + if nums[mid] < target: + left = mid + 1 + else: + right = mid + return left +``` + From d566e73372c9ee2cf8cb3e9bf7003b515b2d11f8 Mon Sep 17 00:00:00 2001 From: Ayato Hayashi Date: Wed, 13 Mar 2024 11:35:52 +0900 Subject: [PATCH 2/5] Update 35. Search Insert Position.md --- 35. Search Insert Position.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/35. Search Insert Position.md b/35. Search Insert Position.md index 618fc44..edd2ba5 100644 --- a/35. Search Insert Position.md +++ b/35. Search Insert Position.md @@ -17,6 +17,27 @@ class Solution: return left ``` +> コードはいいと思います。 +これ、どう考えるといいかなあと思っています。 +n 個要素があると、植木算で n + 1 個の切れ目がありますね。 +そのうち、どこで切ると、 +右はすべて、target <= nums[i] で、 +左はすべて、nums[i] < target となるか、ということですね。 +これを answer とでもしましょう。 +だから、left と right は実は閉区間で、一致するまで回さないといけませんね。 +mid = (left + right) // 2 +とすると、切り捨てられるので、 +left <= mid < right +になります。 +nums[mid] < target +が判明すると、 +mid < answer が分かります。 +一方、 +target <= nums[mid] +が判明すると、 +answer <= mid +が分かりますね。 + 2nd bisectモジュール。 bisect_leftの実装はここ。https://github.com/python/cpython/blob/cfbdce72083fca791947cbb18114115c90738d99/Lib/bisect.py#L74 From 064fca989bc4ecf9c7bce70237524a3e7ab1a21a Mon Sep 17 00:00:00 2001 From: Ayato Hayashi Date: Mon, 6 May 2024 19:27:41 +0900 Subject: [PATCH 3/5] Update 35. Search Insert Position.md --- 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 edd2ba5..1caf72b 100644 --- a/35. Search Insert Position.md +++ b/35. Search Insert Position.md @@ -77,3 +77,19 @@ class Solution: return left ``` +4th + +```python +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + low = 0 + high = len(nums) + while low < high: + middle = (low + high) // 2 + if nums[middle] < target: + low = middle + 1 + else: + high = middle + return low +``` + From f5a54da31b7138303a6c1234a4d28c55f1090678 Mon Sep 17 00:00:00 2001 From: Ayato Hayashi Date: Fri, 8 Nov 2024 11:14:31 +0900 Subject: [PATCH 4/5] Create java.md --- java.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 java.md diff --git a/java.md b/java.md new file mode 100644 index 0000000..a398ba0 --- /dev/null +++ b/java.md @@ -0,0 +1,18 @@ +`Arrays.binarySearch`を使った解法。 +keyが存在しない場合は`- (insertion point) - 1`が返るようになっていて、マイナス値であることが保証されている。 +Pythonの`bisect`モジュールのように素直にinsertion pointを返す実装とどっちが良いかはケースバイケースか。 + + +https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#binarySearch-int:A-int- + +```java +class Solution { + public int searchInsert(int[] nums, int target) { + int ip = Arrays.binarySearch(nums, target); + if (ip >= 0) { + return ip; + } + return -ip - 1; + } +} +``` From d74eb3f8e50d98df672b127f4ee0cb39ffde2bea Mon Sep 17 00:00:00 2001 From: Ayato Hayashi Date: Sat, 9 Nov 2024 15:50:29 +0900 Subject: [PATCH 5/5] Update java.md --- java.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/java.md b/java.md index a398ba0..9b0e9bd 100644 --- a/java.md +++ b/java.md @@ -16,3 +16,20 @@ class Solution { } } ``` + +ビット反転で良い。 +2の補数 -> ビット反転をして1を足す + +ipをマイナスして、1を引く +-> ipの2の補数を求めて、1を引く +-> ipのビット反転をして、1を足して、1を引く +-> ipのビット反転をする + +```java +class Solution { + public int searchInsert(int[] nums, int target) { + int ip = Arrays.binarySearch(nums, target); + return ip >= 0 ? ip : ~ip; + } +} +```