【问题标题】:Explain the O(N) time complexity of the algorithm解释算法的O(N)时间复杂度
【发布时间】:2018-10-31 15:11:09
【问题描述】:

谁能解释以下算法的 O(N) 时间复杂度:

int count = 0;
for (int i = N; i > 0; i /= 2) {
    for (int j = 0; j < i; j++) {
        count += 1;
    }
}

【问题讨论】:

标签: algorithm time-complexity


【解决方案1】:

count的增量数为N+N/2+N/4+N/8+...&lt;2N

【讨论】:

    【解决方案2】:

    如果您递归地记下时间复杂度,您将得到T(n) = T(n/2) + n。使用主定理可以得到结果,如c = log_2(1) = 0n = \Omega(n^c)(主定理的第三种情况)。因此,T(n) = \Theta(n)T(n) = O(n)

    【讨论】:

      猜你喜欢
      • 2015-06-12
      • 2015-05-25
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      相关资源
      最近更新 更多