英文:
What will be the time complexity of this algorithm
问题
我对竞技编程和大O符号都非常陌生。
public void function(int n){
for(int i = n; i > 0; i/=3){
for(int j = 0; j < i; j++){
System.out.println("Hello");
}
}
}
这是算法。
就我所知,时间复杂度定义了运行时间如何受输入数量的影响。
所以在这里举个例子,
如果'n'是10。
外层循环运行log n次,内层循环运行'i'次。
内层循环相对于'i'而非'n'而运行。
所以我在这里有些困惑,关于如何计算时间复杂度。
我认为它是O(log n)。如果我错了,请纠正我。
它会是O(log n)还是O(n log n)还是(n^2)呢?
请帮我解答一下。
谢谢。
英文:
I am very new to competitive programming and to Big O notation.
public void function(int n){
for(int i = n; i > 0; i/=3){
for(int j = 0; j < i; j++){
System.out.println("Hello");
}
}
}
This is the algorithm.
As far as i know about time complexity.It defines how run time gets affected from number of inputs.
So here if we take a example
if 'n' is 10.
The outer loop runs log n times and the inner loop runs 'i' times.
the inner loop runs relatively to 'i' not 'n'.
So im a bit confused here as to how the time complexity is calculated.
I think it is O(log n).Please correct me if i am wrong.
Will it be O(log n) or O (n log n) or (n^2).
Please help me out with this.
Thank you.
答案1
得分: 5
我会尽量用最简单的术语来解释它。
外部循环将简单地以以3为底的log(n)运行三次。
由于i每次减少3的因素,总工作量等于:
n + n/3 + n/9 + n/27 + .... n/(3^log(n))
因为n/3 + ... + n/(3^log(n))始终小于n
例如,假设n = 100
然后,100 + 100/3 + 100/9 + 100/27 + ... = 100 + (33.3 + 11.11 + 3.7 + ...)
我们可以清楚地看到括号中的项始终小于100
整体解决方案的总时间复杂度将为O(n)。
英文:
I will try to explain it in the simplest term possible
The outer loop will simply run log(n) with base 3 times.
Since, i is decreasing by factor of 3 every time. The total work done is equal to :
n + n/3 + n/9 + n/27 + .... n/(3^log(n))
since, n/3 + ... + n/(3^log(n)) will always be less than n
for e.g. let n = 100
then, 100 + 100/3 + 100/9 + 100/27 + ... = 100 + (33.3 + 11.11 + 3.7 + ...)
we can clearly see the terms in the bracket will always be less than 100
The total time complexity of the overall solution will be O(n).
答案2
得分: 1
Actually it will never terminate cause i=0
and update is i *= 3
so i
will stay 0
so we can say O(+oo)
assuming you meant for(int i =1...
instead, then its O(n)
:
- Outer loop is clearly
O(log_3 n)
cause we keep multiplying by 3 - Inner loop will get executed
O(log_3 n)
times with iteration count of(1 + 3 + 9 + 27 + ... + 3^log_3(n))
which is clearly a geometric progression, solving which gives us approx3^log_3(n))
which according to log rules givesn
so this loop takesO(n)
for all iterations, so total complexity isO(n)
英文:
Actually it will never terminate cause i=0
and update is i *= 3
so i
will stay 0
so we can say O(+oo)
assuming you meant for(int i =1...
instead, then its O(n)
:
- Outer loop is clearly
O(log_3 n)
cause we keep multiplying by 3 - Inner loop will get executed
O(log_3 n)
times with iteration count of(1 + 3 + 9 + 27 + ... + 3^log_3(n))
which is clearly a geometric progression, solving which gives us approx3^log_3(n))
which according to log rules givesn
so this loop takesO(n)
for all iterations, so total complexity isO(n)
答案3
得分: 1
以下是翻译好的内容:
对于你的代码:
for(int i = n; i > 0; i/=3){
for(int j = 0; j < i; j++){
System.out.println("Hello");
}
}
内循环变量 j 依赖于外循环变量 i,因此内循环将决定你的算法的复杂性。
因为在第一次运行时,j 会运行 'n' 次,在第二次运行时会运行 'n/3' 次,依此类推... 因此你的总复杂性可以计算为
n + n/3 + n/9 + n/27 + .......
结果为 O(n)。
英文:
for your code :
for(int i = n; i > 0; i/=3){
for(int j = 0; j < i; j++){
System.out.println("Hello");
}
}
Inner loop variable j is dependent on outer loop variable i, so your inner loop will be the one which will decide the complexity for your algorithm.
since j will run 'n' times in first run, 'n/3' times in second run and so on.. therefore your total complexity can be calculated as
n + n/3 + n/9 + n/27 + .......
resulting in O(n)
答案4
得分: 1
这是一个很好的问题!这是一个有点需要更多思考才能分析的棘手问题。
如一些其他答案中正确说明的那样,外部循环:
for(int i = n; i > 0; i/=3)
会运行log(n)次。具体来说是log_3(n)次,但在大O符号中,我们通常不太关心底数,所以log(n)就可以了。
现在内嵌循环有点复杂:
for(int j = 0; j < i; j++){
乍一看,你可能会认为这是一个简单的log(n)循环,但让我们再仔细看看。
在第一次迭代中,这将运行N次,因为i的值将为n。接下来的迭代将运行n/3次。然后是n/9、n/27、n/81等等...
如果我们对这个级数求和,很明显可以看到它的总和会小于2n。
因此,我们可以得出结论,这个算法的复杂度是O(n)。
英文:
So this is a great question! It's a tricky one that takes a little more thinking to analyse.
As correctly stated in some of the other answers, the outer loop:
for(int i = n; i > 0; i/=3)
Will run log(n) times. Specifically log_3(n) times but in big O notation we don't often worry about the base so log(n) will be fine.
Now the nested loop is a bit trickier:
for(int j = 0; j < i; j++){
On first glance you may think this is a simple log(n) loop but lets look a little further.
So on the first iteration this will run N times since the value of i will be n. Next iteration it will be run n/3 times. Then n/9, n/27, n/81 etc....
If we sum this series, it is clear to see it will total less than 2n.
Therefore we can conclude this algorithm has a complexity of O(n).
答案5
得分: 0
在你的代码片段中:
for (int i=0; i < n; i*=3) {
for (int j=0; j < i; j++) {
System.out.println("Hello");
}
}
外部循环中的 i
复杂度为 O(log_3(n))
,因为每次循环增加一次,将 i
达到 n
需要的迭代次数会减少3的倍数。这是对数行为(在这种情况下是log_3
)。内部循环中的 j
仅迭代与外部的 i
值相同的次数,因此我们可以将外部复杂度的平方,得到:
O(log_3(n)^2)
英文:
In your code snippet:
for (int i=0; i < n; i*=3) {
for (int j=0; j < i; j++) {
System.out.println("Hello");
}
}
The outer loop in i
is O(log_3(n))
, because each increment of the loop decreases the amount of ground needed for i
to reach n
by a factor of 3. This is logarithmic behavior (log_3
in this case). The inner loop in j
simply iterates the same number of times as whatever the outer value of i
might be, so we can just square the outer complexity, to arrive at:
O(log_3(n)^2)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论