Skip to content

Create 153. Find Minimum in Rotated Sorted Array.md#44

Merged
Mike0121 merged 2 commits intomainfrom
Mike0121-patch-1
Nov 1, 2025
Merged

Create 153. Find Minimum in Rotated Sorted Array.md#44
Mike0121 merged 2 commits intomainfrom
Mike0121-patch-1

Conversation

@Mike0121
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

下で考えるうえで True False の境界を探しましたが、別にここはブーリアンである必要はありません。
https://discord.com/channels/1084280443945353267/1230079550923341835/1233971372946882600

Comment on lines 29 to 31
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

クエリが nums[x] < nums[-1]だと、
[false, false, ..., false, true, true, ..., true, false] の左の切れ目を探してることになりますかね。

また[2, 1]という入力の場合、二分探索一回目でleft=0, right=2, mid=1となりnums[mid] == nums[-1] なのでrightがデクリメントされますが、
その瞬間は半開区間だけどrightの位置に求める答えがあって違和感があります。その後leftを区間から右にはみ出させて答えに持っていってますがちょっと無理矢理な感じがしました。

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.

fhiyoさん、コメントありがとうございます。
はい、下記の方が文言としては正確ですね。

クエリが nums[x] < nums[-1]だと、
[false, false, ..., false, true, true, ..., true, false] の左の切れ目を探してることになりますかね。

1回目の解法は思いついたままに書いてしまったため、
右端2つまで絞られた際にご指摘のとおり読み手からするとかなり無理矢理かと思います。
半開区間による解法を4回目として改めて追記しました。

(ややコメントの理解に不安があるので、違和感を感じたら教えてください。)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nums全体に対するクエリでは無く、探索範囲に対するクエリと見る感じなんですかね。

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.

はい、書き換え可能と思って書いてみましたが、nums[-1]の方がわかりやすいですね。

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.

下記でも同様の議論がありました。
https://discord.com/channels/1084280443945353267/1230079550923341835/1233971068952121354

あと、nums[right] と比較してますが、nums[-1] でもこの問題の場合はよくないですか?
いましていることをメタ的にいうと、選択肢、つまり、考察の幅自体を広げたいと思っています。

nums[0] <= nums[i] な領域と nums[0] > nums[i] な領域の境界を探せ
nums[-1] < nums[i]  な領域と nums[-1] >= nums[i] な領域の境界を探せ

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がFalseになるのでそこを特別に考えなければならず、自分は割と考えるのが大変でした...閉区間でも nums[mid] <= nums[-1] で二分探索すれば良いんじゃないかという気がします。

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.

ありがとうございます。ご指摘の通りですね。
また、fhiyoさんのPRにあったクエリとして今考えていることがなにかを明確にするのは非常に良いと思ったので、参考にします。

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.

改めて見たら、2, 3回目が半開区間ではなく閉区間になってますね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

クエリがnums[x] > nums[-1]なので、
[true, true, ..., true, false, false, ..., false] のfalseの左端にある切れ目を探してることになりますね。

if nums[mid] <= nums[-1]:
    right = mid
else:
    left = mid + 1

と同じことではあるんですが、

[false, false, ..., false, true, true, ..., true] の切れ目を探す。最終的なleftが切れ目になる。

と上の方に書いているのにあえて変化させるのはなぜだろう?とは感じます

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.

すみません、非常に納得いきました。考え方の[false, false, ..., false, true, true, ..., true]の方との対応を意識していませんでした。解き直し時に意識してみます。ありがとうございます!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あえて1行で2つの変数に代入する必要はなさそうです。以下のようなコメントを以前もらったので共有します。

thonda28/leetcode#3 (comment)

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.

ありがとうございます。自分も1つ前の問題で同様の指摘を頂いたため、step4にて修正しました。頭に入れておきます。

@Mike0121 Mike0121 changed the title 153. Find Minimum in Rotated Sorted Array Create 153. Find Minimum in Rotated Sorted Array.md Nov 1, 2025
@Mike0121 Mike0121 merged commit fffa163 into main Nov 1, 2025
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.

4 participants