Conversation
| # self.right = right | ||
| class Solution: | ||
| def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: | ||
| # complete binary tree |
| # self.right = right | ||
| class Solution: | ||
| def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: | ||
| left, mid, right = 0, len(nums) // 2, len(nums) |
There was a problem hiding this comment.
好みかもしれませんが、3変数横並びだと少し認知負荷あるかと思いました。
midは次の行でも良い気もします。
There was a problem hiding this comment.
確かに演算も入っているし見にくかったかもしれませんね。。
ありがとうございます!
There was a problem hiding this comment.
野田さんから以下のようなコメントをもらったことがあるので、ただの代入であればそれぞれで宣言したほうがよいかもです。
|
|
||
| 時間計算量:O(N) | ||
|
|
||
| 空間計算量:O(NlogN) |
There was a problem hiding this comment.
考え方間違ってたらすみません。以下のように考えました。
再帰1回目:
nums[:root_value_index]とnums[root_value_index + 1:]でサイズN-1のコピーが発生
再帰2回目:
同様にサイズN-2のコピーが発生
...
再帰k回目:
同様にサイズN-kのコピーが発生
k=logNなので、\sum_{i=1}^k (N-i) = kN - \frac{k(k+1)}{2} = NlogN - \frac{logN(logN+1)}{2}
There was a problem hiding this comment.
毎回N-(2^k-1)のコピーが発生して、
\sum_{i=1}^k (N-(2^i-1)) = Nk + k + 2(2^k-1)
かな。。
There was a problem hiding this comment.
再帰 1 回目: N / 2
再帰 2 回目: N / 4
再帰 3 回目: N / 8
...
で、 N / 2 + N / 4 + N / 8 + ... <= N だと思いました。
There was a problem hiding this comment.
あ、ようやく理解しました。
私はleftとrightを並列で計算してましたね。。
| nums_queue = deque(nums) | ||
| def set_values(cbt_root: Optional[TreeNode]) -> None: | ||
| nodes = [cbt_root] | ||
| while nodes: |
There was a problem hiding this comment.
これnodesとwhileいらなくないでしょうか?
nodesにcbt_root以外入らないような気がしてます
There was a problem hiding this comment.
ご明察です。
書き直しました。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
# complete binary tree
def build_cbt(index: int):
if index >= len(nums):
return None
root = TreeNode()
root.left = build_cbt(2 * index + 1)
root.right = build_cbt(2 * index + 2)
return root
nums_queue = deque(nums)
def set_values(cbt_root: Optional[TreeNode]) -> None:
if not cbt_root:
return None
set_values(cbt_root.left)
cbt_root.val = nums_queue.popleft()
set_values(cbt_root.right)
cbt_root = build_cbt(0)
set_values(cbt_root)
return cbt_root|
|
||
| 計算量:nums.length=Nとして、 | ||
|
|
||
| 時間計算量:O(N) |
There was a problem hiding this comment.
各レベルでO(n/2)のコピーコストが発生して、O(n log n)になりませんか?
There was a problem hiding this comment.
なんだかよく分からなくなってしまいました。。
時間がO(NlogN)で空間がO(N)でしょうか?
#26 (comment)
ちょっと考えます。
There was a problem hiding this comment.
コピーコストの部分は
#26 (comment)
と同様、
再帰 1 回目: N / 2
再帰 2 回目: N / 4
再帰 3 回目: N / 8
...
で、 N / 2 + N / 4 + N / 8 + ... <= N だと思いました。
で、 O(N) だと思いました。
There was a problem hiding this comment.
上のコメント間違えました。 @kazukiii さんのおっしゃる通り、各レベル k で N / 2^k * 2^k = N のコピーコストが発生し、レベルの数が log N なので、O(N log N) だと思います。
There was a problem hiding this comment.
纏めると、
時間計算量:0(NlogN)
空間計算量:0(N)
インデックス管理方式にすると
時間計算量:0(N)
空間計算量:0(logN)
ですかね。
| class Solution: | ||
| def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: | ||
| def build_bst(left: int, right: int): | ||
| if left >= right: |
There was a problem hiding this comment.
[質問]
半開区間で考えているときrightがleftより左に来ることはないので、個人的にはleft == rightと書きたくなりますが、
ここの部分どう考えているか知りたいです。
There was a problem hiding this comment.
あまり深い拘りはないのですが、どこかのコメントでこちらの書き方の方が親和性があるとあったので採用しました。
確かにleft == rightで止まる実装にはなってるんですけど、そこまで読まなくても、left > rightになる場合大丈夫かしらと不安になる必要がなくなるからでしょうか。
There was a problem hiding this comment.
なるほど、読み手の安心感という観点ですね。自分もこの書き方を使っていこうと思います。
There was a problem hiding this comment.
安心感というよりは、上から読んでいったときに、left > right の場合がないと確定するのは、sortedArrayToBST の定義が終わった時になりますね。しかも、数学的帰納法を利用して考えることになります。
読む人の「心の理論」
https://discord.com/channels/1084280443945353267/1225849404037009609/1234206158630289450
パズルを解かせる
https://discord.com/channels/1084280443945353267/1200089668901937312/1211248049884499988
There was a problem hiding this comment.
ありがとうございます、納得しました。
以前下の「パズルを解かせる」を読んだのですが、この感覚まだ染み付いていませんでした。。
| return None | ||
|
|
||
| root = TreeNode() | ||
| node_range_pairs = [(root, (0, len(nums)))] |
There was a problem hiding this comment.
好みですが、ちょっと同じ行にカッコが多いなと思いました。leftとrightはまとめなくても良いかもです。
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/