-
Notifications
You must be signed in to change notification settings - Fork 0
108. Convert Sorted Array to Binary Search Tree #26
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
| 15分ほど | ||
|
|
||
| 時間計算量O(n) | ||
| 空間計算量O(n) |
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.
(nがnumsの長さなら) O(log n) じゃないですか?
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.
@fhiyo
レビューありがとうございます。
1回目の呼び出しn/2、2回目で n/4。。。n/8 n/16となっていくのでO(log n)ですね。
失礼しました。
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.
ん, 和をとったらnではないですか?
| ・関数内で値を書き換えないものをconstに変更 | ||
| 参照にしているがpointerとintはそこまでコストは高くない | ||
| ・バイナリーツリーを作る関数からの返却値を変数に入れていたけどそのままreturnへ | ||
| 制約上、numsが0になることはないので頭に置いている判定はいらないか |
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の長さが0の場合でもステップ2の CreateTree(nums, 0, 0) はnullptrを返すので不要なのかなと思います。制約外だけど有り得そうな入力に対しても対処しておくのは入力の仕様が変わったときに修正せずに済むかもしれないので良いんじゃないかなと (程度問題かもしれないですが)
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.
| 閉区で重なっていた部分が探索範囲から除外されて、無駄な探索を減らせること(?) | ||
|
|
||
| queueに入れてloopで解く方法もある | ||
| ↓いつも纏まっていてすごく勉強になる |
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.
ありがとうございます。
励みになります。
|
拝見しました。 |
|
|
||
| private: | ||
| TreeNode* CreateTree(const vector<int>& nums, const int& start, const int& end) { | ||
| if (start == end) { |
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.
以前このような議論がありましたので、ご参考まで。
TORUS0818/leetcode#26 (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.
@kazukiii
ありがとうございます。
一連のやり取りとDiscord読みました。レビューする側の視点まで意識できていないようです。
この視点なかったので勉強になります!
| } | ||
|
|
||
| private: | ||
| TreeNode* CreateTree(const vector<int>& nums, const int& start, const int& end) { |
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.
intは基本4bytesなので値渡しで良さそうです。
参考) kazukiii/leetcode#16 (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.
@kazukiii
なんでもかんでも参照がいいってわけでは無いのですね。
この章読んだことがあるのに身についておりませんでした。読み直します。
| } | ||
|
|
||
| private: | ||
| TreeNode* CreateTree(vector<int>& nums, int start, int end) { |
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.
リーダブルコードという多くのエンジニアが読んでいるような有名な本があり、その中に start(begin) - end はC++での慣習から終端がexclusiveな範囲を表すという記述があるので、頭に入れておくといいかもしれません。
こちらの本です -> リーダブルコード
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.
こちらも読んだのはもう2年以上前で完全に抜け落ちているので読み直します!
| 15分ほど | ||
|
|
||
| 時間計算量O(n) | ||
| ~~空間計算量O(n)~~ |
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 のコピーをキューに入れているため、空間計算量は O(n) だと思います。
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.
コメントがややこしかったですね。こちらは再帰に対する計算量ですのでlog nだと思いました。
queueを使った側の計算量記載しておりませんでした🙇
| この問題を読んですぐに思いついたのは再帰で解く方法 | ||
| 15分ほど | ||
|
|
||
| 時間計算量O(n) |
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.
1 段目は 長さ n の配列のコピーを 1 個作成、
2 段目は 長さ n / 2 の配列のコピーを 2 個作成、
3 段目は 長さ n / 4 の配列のコピーを 4 個作成、
…
と続き、これが log n 段あるため、時間計算量は O(n log n) だと思いました。
| } | ||
|
|
||
| private: | ||
| struct NodeAndNums { |
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.
NodeAndNums には、ノードと、 nums 内の区間を表すインデックス 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://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/
問題文(プレミアムの場合)
備考
次に解く問題の予告
Path Sum
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、loop.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。