以下功能的时间复杂度是多少?

void fun(int n, int k)
{
    for (int i=1; i<=n; i++)
    {
      int p = pow(i, k);
      for (int j=1; j<=p; j++)
      {
          // Some O(1) work
      }
    }
}

  

上述函数的时间复杂度可以写为1 k + 2 k + 3 k + ... n1 k。

让我们试试几个例子:

k =
Sum =  +  +  ... n
    = n(n + )/
    = n 

k =
Sum =   +   +   + ... n1 。
    = N(N + )(2N + )/
    = N + N + N/ 

K =
Sum=   +   +   + ... N1 。
    = N)
    = N +N +N 

通常,渐近值可以写为(nk+ 1)/(k + 1)+Θ(nk

请注意,在像Θ这样的渐近符号中,我们总是可以忽略低阶项。

所以时间复杂度是Θ((n k + 1 )/(k + 1))。

Time Complexity of Loop with Powers的更多相关文章

  1. Leetcode: Circular Array Loop

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  2. Instant Complexity - POJ1472

    Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Description Analyzing the run-time comple ...

  3. 三部曲二(基本算法、动态规划、搜索)-1004-Instant Complexity

    Instant Complexity Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) ...

  4. The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near

    The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near ...

  5. Instant Complexity(模拟,递归)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1535   Accepted: 529 Description Analyz ...

  6. [LeetCode] Circular Array Loop 环形数组循环

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  7. [Swift]LeetCode457. 环形数组循环 | Circular Array Loop

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  8. Use JPath but not recursively loop a JObject to modify the values.

    I am dealing with a Json file, I parsed it into jObject, I have another list which flattened the pro ...

  9. [LeetCode] 457. Circular Array Loop 环形数组循环

    You are given a circular array nums of positive and negative integers. If a number k at an index is ...

随机推荐

  1. 读Kafka Consumer源码

    最近一直在关注阿里的一个开源项目:OpenMessaging OpenMessaging, which includes the establishment of industry guideline ...

  2. TCP/IP 协议栈 ------ ICMP

    I C M P经常被认为是 I P层的一个组成部分.它传递差错报文以及其他需要注意的信息.I C M P报文通常被I P层或更高层协议( T C P或U D P)使用.一些I C M P报文把差错报文 ...

  3. 导入Excel 文件(图片和文字)NPOI+BootStrap fileinput 插件 的结合使用

    1.页面代码: <!DOCTYPE html><html><head> <meta name="viewport" content=&qu ...

  4. 使用echarts,制作色温图

    1.需要下载echarts的echarts-all.js文件和创建地图需要用到的数据源 2.在项目中创建jsp文件,将js文件引入 <script type="text/javascr ...

  5. Thinkphp高仿陌陌网页直播

    此项目利用 TP+Redis+Nginx+nginx-rtmp-module+ffmpeg+HLS +Swoole 的架构方案 GitHub 地址:https://github.com/DOUBLE- ...

  6. 大数据学习系列之二 ----- HBase环境搭建(单机)

    引言 在上一篇中搭建了Hadoop的单机环境,这一篇则搭建HBase的单机环境 环境准备 1,服务器选择 阿里云服务器:入门型(按量付费) 操作系统:linux CentOS 6.8 Cpu:1核 内 ...

  7. 洛谷 P3927 Factorial

    题目描述 SOL君很喜欢阶乘.而SOL菌很喜欢研究进制. 这一天,SOL君跟SOL菌炫技,随口算出了n的阶乘. SOL菌表示不服,立刻就要算这个数在k进制表示下末尾0的个数. 但是SOL菌太菜了于是请 ...

  8. LintCode-买卖股票的最佳时机

    如果有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多仅仅同意完毕一次交易(比如,一次买卖股票),设计一个算法来找出最大利润. 您在真实的面试中是否遇到过这个题? Yes 例子 给出 ...

  9. Android音频: 怎样使用AudioTrack播放一个WAV格式文件?

    翻译 By Long Luo 原文链接:Android Audio: Play a WAV file on an AudioTrack 译者注: 1. 因为这是技术文章,所以有些词句使用原文,表达更准 ...

  10. Woody的Python学习笔记1

    Python 是一种解释性语言:这意味着开发过程中省去了编译这个环节,类似于PHP\Perl. Python 是交互式语言:这意味着你能够在一个python提示符,直接互动运行写你的程序. Pytho ...