-
Notifications
You must be signed in to change notification settings - Fork 0
35. Search Insert Position #28
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
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.
hayashi-ay
left a comment
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.
良いと思います。
|
|
||
| - smallerの設定を0にしてしまった。最初の要素がtargetより大きかったら意味ない | ||
| - larger_or_equalを返すかsmallerを返すかでindexの計算がこんがらがり頭を使った、苦労した | ||
| - smallerに-1を使うのがお行儀が悪いのかはよくわからない???(Atcoderとかのコードだとよくあるけどやばいのかも) |
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.
AtCoderやLeetCodeで良く見るコードがアプリケーションエンジニアとして良いコードではないなという所感があります。
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.
これは開区間でやっていることに相当するので、分かってやっているならいいですが、分からずやっているととにかく怖いです。数学でもあるんですよ。なんか証明はそれっぽいが違和感がある。ちょっと質問してみると何も分かっていない事が分かるというのが。
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.
ありがとうございます。あーそれでいうと、過去にAtcoderで自分が書いてたときに思ってたのは、「とりあえずleftのindexを小さくしたいけど、小さくしすぎるとmidが範囲外になるしこれくらいにしとくか」だったので全然わかってなかったですね…
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.
smaller < answer <= larger_or_equal を満たすというのが不変条件ですね。answer は配列の中で同じ値またはそれ以上となる一番左の位置です。知りたいのは分かって書いているかです。
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.
https://qiita.com/drken/items/97e37dd6143e33a64c8c
なんか過去に読んだ記事で「めぐる式二分探索」と言うのがあって、最初に自分が書いたときもそれで、「rightは常に満たす」「leftは常に満たさない」と言う考えのもと、やってました(この記事の2章)
なのでこれを書いたときには、閉区間というイメージは頭の中になかったです
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.
分かっていて書いているならば別にいいと思うんですが、以下のことを思います。
- 分かっていないことは、文字からははっきりしなくても、話をするとかなり簡単に分かります。
- 特に分からずに覚えていて、その場で説明ができないとコードを読む能力が低いことを示しています。
- 読む力のほうがだいぶ大事です。
https://www.ms.u-tokyo.ac.jp/~yasuyuki/sem.htm
まず,当然書いてあることを理解することが第一歩です.黙って「何々である」とか,"It is easy to see...", "We may assume that...", "It is enough to show..."などと書いてあるのはすべて,なぜなのか徹底的に考えなくてはいけません.「本に書いてあるから」とか「先生がそう言うから」などの理由で,なんとなく分かったような気になるのは絶対にアウトです.そういうところは「なぜですか」と聞かれるに決まっているんですから,どうきかれてもすぐに答えられるように準備をしておく必要があります.
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.
ちゃんとわかって説明できるかと、読む能力との関係が深いことが初耳でした。説明できないのは、頭の中でコードがちゃんと動くことをシミュレーションできないと言うことで、それは読めてない、と言うことでしょうか。
確かに最初にこれを覚えたときはそのままとりあえず書き方を丸暗記していて、よくわかっていなかったかもしれません。
| def searchInsert(self, nums: List[int], target: int) -> int: | ||
| left = 0 | ||
| right = len(nums) | ||
| while right > left: |
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.
個人的には while left < right:と左側に小さい値が来る方が自然なのですが、右側通行、左側通行みたいに決めの問題なのかもしれないです。とはいえ、コードとしてもleft, rightの準備に並ぶ方が自然な気もしています。
| - elifよりelseが良い | ||
| - https://github.com/SuperHotDogCat/coding-interview/pull/9/files | ||
| - if文、nums[mid] < targetという向きの方がtarget > nums[mid]よりスッキリするかも | ||
| - whileの中もright > leftがいいかも |
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.
個人的には「右」という変数が左側にあるのは特殊な状況に見えます。
while left < right の方が個人的には混乱少なく読めますね
|
|
||
| - 二分探索について。しっくりできていない。[この辺の議論](https://discord.com/channels/1084280443945353267/1192736784354918470/1199018938005213234)を拝見。 | ||
| - 区間で考えるというアイデアがなかった | ||
| - 理解に時間がかかったが 閉開区間の場合 |
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.
半開区間でしょうか…。
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.
検索すると用例が数例ありましたが、ほぼ使わない言葉な気がしますね。
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.
なるほど、そうなんですね、ありがとうございます
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.