Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Python3/153. Find Minimum in Rotated Sorted Array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
## Step 1. Initial Solution

- 二分探索的にやるにはどうすれば良いか
- どこかの基準と比較?
- 不等号の関係性でどこに最小値あるか分かる
- 最初の要素が最小値の場合は例外処理
- もっといい方法があれば後で実装する

```python
class Solution:
def findMin(self, nums: List[int]) -> int:
if nums[0] < nums[-1]:
return nums[0]
left = 0
right = len(nums) - 1
while left + 1 < right:
middle = (left + right) // 2
if nums[left] < nums[middle]:
left = middle
else:
right = middle
return nums[right]

```

### Complexity Analysis

- 時間計算量:O(log n)
- 空間計算量:O(1)

## Step 2. Alternatives

- 最大値のところを踏み越えて最小値に乗ったら返すという方が自然かもしれない
- https://github.com/tokuhirat/LeetCode/pull/42/files#diff-88c7c13d92168adf69c9cdf4e197359b856835b1b48d08927346a143bf8f7a55R17
- https://github.com/olsen-blue/Arai60/pull/42/files#diff-856251eccb601f9962fc7fdd308675f5690975413b45fe6be0950672570bc6caR44
- これをもとになるべくシンプルに書く
- 場合分けすると確かにこれで十分

```python
class Solution:
def findMin(self, nums: List[int]) -> int:
left = 0
right = len(nums) - 1
while left < right:
middle = (left + right) // 2
if nums[middle] < nums[right]:
right = middle
else:
left = middle + 1
return nums[left]
```

- リスト内に重複がある場合はどうするか
- http://github.com/hroc135/leetcode/pull/40/files#diff-04f2110efff2f6666fa57b541ac1fa1b1de77a8edc09704f6266cc71cb7420c6R110
- https://github.com/olsen-blue/Arai60/pull/42/files#diff-856251eccb601f9962fc7fdd308675f5690975413b45fe6be0950672570bc6caR82
- 二分探索だと重複要素のどれに最初に当たるか分からない
- 基本は後ろ側を返すような処理になっている
- 見ている範囲(left, right, middle)で大小関係が分からない可能性がある
- 最初のif文でrightを更新すると戻れない
- 同じような形式で実装するのは難しそう

## Step 3. Final Solution

- シンプルな実装に落ち着いた
Copy link

Choose a reason for hiding this comment

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

探す値が変わるのは二分探索っぽくないないように感じました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにnums[right]ではなく、nums[0]やnums[-1]で固定した方が考える負荷が少し下がるかもしれないですね。


```python
class Solution:
def findMin(self, nums: List[int]) -> int:
left = 0
right = len(nums) - 1
while left < right:
middle = (left + right) // 2
if nums[middle] < nums[right]:
right = middle
else:
left = middle + 1
return nums[left]
```
Copy link

@fuga-98 fuga-98 Aug 14, 2025

Choose a reason for hiding this comment

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

別のところで講師の方されていた質問です。考えてみると理解が深まるかもしれません!

  1. 「2で割る処理がありますがこれは切り捨てでも切り上げでも構わないのでしょうか。」
  2. 「nums[middle] <= nums[right] とありますが、これは < でもいいですか。」
  3. 「nums[right] は、nums[-1] でもいいですか。」
  4. 「right の初期値は nums.size() でもいいですか。」

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

  1. この実装なら問題なさそうです。切り下げで上手くいかないのは、下から詰める処理が先に行われてしまうとき。切り上げが上手くいかないのは、上から詰める処理が先に行われてしまうとき。ということだと思うのでここだけ気を付けます。
  2. =が入っている方を先に処理する場合は、1.のところで考えたことを同じように意識します。今回は切り下げなら問題ないですが、切り上げなら=を入れられないです。
  3. 問題ないです。
  4. 上側の更新をmiddle + 1にして、終了条件をleft +1 == rightの時にすれば行けそうです。