简体   繁体   English

计算该算法的时间复杂度

[英]Calculating time complexity of this algorithm

So I was doing this question on Leetcode, where given the inorder and preorder traversal of a binary tree, we have to build the tree.所以我在 Leetcode 上做这个问题,给定二叉树的中序和前序遍历,我们必须构建树。 So I wrote the code, and the solution is working, but I am a bit stuck while calculating time complexity of the solution.所以我编写了代码,并且解决方案正在运行,但是在计算解决方案的时间复杂度时我有点卡住了。 Could someone please provide an insight into calculating it?有人可以提供计算它的见解吗? Any input is appreciated.任何输入表示赞赏。 Here is the code:这是代码:

class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
    def construct(preord, inord, noted):
        if preord == []:
            return

        root = TreeNode(preord[0])
        noted.add(preord[0])
        index = inord.index(preord.pop(0))
        
        for i in inord[index::-1]:
            if preord != [] and i == preord[0]:
                root.left = construct(preord, inord, noted)

        for i in inord[index + 1:]:
            if i in noted:
                break
            if preord != [] and i == preord[0]:
                root.right = construct(preord, inord, noted)
        return root
    
    visit = set()
    root = construct(preorder, inorder, visit)
    return root

Variable declaration and conditionals have all the same time complexity:变量声明和条件具有相同的时间复杂度:

O(1)
This means constant time

Loops or other statements that depend of the quantity of data you have:取决于您拥有的数据量的循环或其他语句:

O(N)

And if you have nested loops or again statement that depend on the data:如果您有依赖于数据的嵌套循环或再次语句:

O(n^k)

K being the number of nested levels you have.

Those are not the only ones, here it's an useful link where you can see them all这些不是唯一的,这是一个有用的链接,您可以在其中查看所有内容

https://www.bigocheatsheet.com/ https://www.bigocheatsheet.com/

Depending of the number of elements you are looping or checking the time incresases or decreases.根据您循环或检查时间增加或减少的元素数量。 When we talk about time complexity we need to always put ourselves in the worst case possible.当我们谈论时间复杂度时,我们需要始终将自己置于最坏的情况下。

Imagine you have 1000 numbers, and you want to check if 5 for example is present, you will need to loop through all of them and check.假设您有 1000 个数字,并且您想检查是否有 5 个,例如,您需要遍历所有数字并检查。

In the worst case possible your program will need to check all of them, that's why a loop is O(n), n being your 1000 numbers.在最坏的情况下,您的程序可能需要检查所有这些,这就是为什么循环是 O(n),n 是您的 1000 个数字。

Going back to your program, you don't have any nested loops, but you have some for loops, so the Big O of your algorithm is O(n).回到你的程序,你没有任何嵌套循环,但你有一些 for 循环,所以你的算法的大 O 是 O(n)。

You don't need to count the if's or any other statement that takes less than O(n).您不需要计算 if 或任何其他少于 O(n) 的语句。

class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
    def construct(preord, inord, noted):
        if preord == []:
            return

        root = TreeNode(preord[0])  //O(1) <--------------------
        noted.add(preord[0])
        index = inord.index(preord.pop(0))
        
        for i in inord[index::-1]:              //O(n) <-------------
            if preord != [] and i == preord[0]:
                root.left = construct(preord, inord, noted)

        for i in inord[index + 1:]:
            if i in noted:             //O(1) <---------------
                break
            if preord != [] and i == preord[0]:
                root.right = construct(preord, inord, noted)
        return root
    
    visit = set()
    root = construct(preorder, inorder, visit)
    return root

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM