Skip to content

Conversation

@Ryotaro25
Copy link
Owner

問題へのリンク
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内に各ステップで感じたことを追記します。

15分ほど

時間計算量O(n)
空間計算量O(n)
Copy link

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) じゃないですか?

Copy link
Owner Author

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)ですね。

失礼しました。

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になることはないので頭に置いている判定はいらないか
Copy link

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を返すので不要なのかなと思います。制約外だけど有り得そうな入力に対しても対処しておくのは入力の仕様が変わったときに修正せずに済むかもしれないので良いんじゃないかなと (程度問題かもしれないですが)

Copy link
Owner Author

Choose a reason for hiding this comment

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

@fhiyo

制約外だけど有り得そうな入力に対しても対処しておくのは入力の仕様が変わったときに修正せずに済むかもしれないので良いんじゃないかなと

なるほどですしっくりきました。
今回は関数の中でも処理しているので、メイン処理側では重複しているので不要でしたね。
修正しました。
5cc38b0

閉区で重なっていた部分が探索範囲から除外されて、無駄な探索を減らせること(?)

queueに入れてloopで解く方法もある
↓いつも纏まっていてすごく勉強になる

Choose a reason for hiding this comment

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

ありがとうございます。
励みになります。

@TORUS0818
Copy link

拝見しました。
良いと思いました。


private:
TreeNode* CreateTree(const vector<int>& nums, const int& start, const int& end) {
if (start == end) {

Choose a reason for hiding this comment

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

以前このような議論がありましたので、ご参考まで。
TORUS0818/leetcode#26 (comment)

Copy link
Owner Author

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) {

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)

Copy link
Owner Author

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) {

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な範囲を表すという記述があるので、頭に入れておくといいかもしれません。
こちらの本です -> リーダブルコード

Copy link
Owner Author

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)~~
Copy link

Choose a reason for hiding this comment

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

最初に nums のコピーをキューに入れているため、空間計算量は O(n) だと思います。

Copy link
Owner Author

@Ryotaro25 Ryotaro25 Aug 20, 2024

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)
Copy link

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 {
Copy link

Choose a reason for hiding this comment

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

NodeAndNums には、ノードと、 nums 内の区間を表すインデックス 2 つを持たせたほうが、配列のコピーが発生せず、時間計算量が小さくなると思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@nodchip
与えられているものをそのまま構造体に落とし込むのではなく、負荷の少ない方法をもっと考えないとでうすね。
インデックスを持たせる方式で実装してみました。
eae9588

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.

7 participants