-
Notifications
You must be signed in to change notification settings - Fork 0
153. Find Minimum in Rotated Sorted Array #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
middle<right<=nums.back()だから、nums[middle]<nums.back()ならnums[middle]<nums[right]だし、 | ||
nums[middle]>nums.back()ならnums[middle]>nums[right]なので | ||
- 個人的には二分探索は調べるべき区間を狭めていくアルゴリズムなので、left~right間で完結させたい気持ちがあり、 | ||
nums[right]を比較に用いる方が好み |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
私の気持ちは、大小関係の真偽をはじめに固定して、一番左の true を探したいと考えたほうが(条件が変わっていくよりも)素直というものです。
このチェックリストよくできていますね。{F,T}^4 の 16通りについて答えてもよいです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
動くものは
(切り上げにする, nums[middle] <= nums[right]で<に変更, nums[right]をnums[len(nums)-1]に変更, rightの初期値をlen(nums)に変更)
= (F, F, F, F),
(F, T, F, F),
(F, T, T, F),
(F, F, T, F),
(F, F, T, T)
だと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
良いと思います。
left = middle + 1 | ||
case nums[middle] == nums[right]: | ||
// This code should be unreachable because all elements of nums are unique and middle<right. | ||
log.Fatal("Something went wrong.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
想定外の入力に対するエラーケースがあるの非常に良いと思いました。
よく考えられており、良いと思いました。(半開区間の不採用の理由(辻褄合わせ?)がちょっと自分には読み取れなかったですが。) |
case nums[middle] > nums[right]: | ||
left = middle + 1 | ||
case nums[middle] == nums[right]: | ||
// This code should be unreachable because all elements of nums are unique and middle<right. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
訂正: "This code" -> "This block"
@Mike0121 反対にinsert positionを探すような際には半開区間を用いても自然な解釈ができると思います。終了条件でleft==rightとなった際に、leftの左側にinsertすれば良い、と解釈できるからです。 |
- https://github.com/seal-azarashi/leetcode/pull/39/files#r1851404872 | ||
- https://discord.com/channels/1084280443945353267/1233295449985650688/1240269414415339571 | ||
- https://github.com/Ryotaro25/leetcode_first60/pull/46#discussion_r1869993674 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
トップダウンというよりも、言葉の定義、たとえば「left, right をなんだと思っているのか」がぶれていないことを気にしています。
たとえば、「left とは、いままで見つかった false の位置の最大」で「right とは、いままで見つかった true の位置の最小」というのでもいいのです。そうすると、left, right が隣り合ったところで探索終了のはずです。
でも、だいたいの場合、「left は、左のあたり」くらいの理解しかしていないので、読んでいくと何を言っているのかが分からなくなります。そんで辻褄合わせに最後になんか場合分けがついたりします。それに加えて、閉区間というワードを言うと納得してもらえるようだという学習をしていて、とりあえず、言ってみたりします。分かって唱えているならばただの情報の圧縮で効率的なコミュニケーションですが分からずに唱えているのは分かります。
どういうものだと考えていてもいいのだから、決めて話してくれればいいんですが。
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/