Quadruple的大O嵌套for循环

时间:2017-05-26 02:36:01

标签: python big-o analysis

我试图找出这个的大O符号是什么。我知道if if = n和that = m,大O是O(mn),但是如果我连续4个像这样,最糟糕的情况下它会贯穿所有这些对我的Big会有什么影响O符号。

def get_winner(self):      
    for this in range(self._this):
        for that in range(self._that - 2):
            # If statement here
                return

    for that in range(self._that):
        for this in range(self._this - 2):
            # If statement here
                return

    for this in range(self._this - 2):
        for that in range(self._that - 2):
            # If statement here
                return

    for this in range(self._this - 2):
        for that in range(self._that - 2):
            # If statement here
                return

2 个答案:

答案 0 :(得分:1)

你只需添加4个不相互嵌套的for循环,最坏的情况是4 *(n * m),即O(4nm)= O(nm)

答案 1 :(得分:0)

它将是O(4mn),但因为对于任何常数a,O(anm) = O(nm),所以最终会O(mn)