简体   繁体   English

给定算法的时间复杂度是多少?

[英]What will be time complexity for the given algorithm?

We were given the problem for which I came up with a solution.我们得到了我想出解决方案的问题。 Could somebody help me identify the time complexity for the given solution?有人可以帮我确定给定解决方案的时间复杂度吗? Should it be O(n) or O(n^2)?应该是 O(n) 还是 O(n^2)? According to me it should be O(n).在我看来,它应该是 O(n)。

Problem问题

Write a program to print the sum of the elements of an array following the given below condition.编写一个程序,按照以下给定的条件打印数组元素的总和。

If the array has 6 and 7 in succeeding orders, ignore the numbers between 6 and 7 and consider the other numbers for calculation of sum.如果数组依次为 6 和 7,则忽略 6 和 7 之间的数字,并考虑其他数字进行总和计算。

Eg1) Array Elements - 10,3,6,1,2,7,9 O/P: 22 [ie 10+3+9] Eg1) 数组元素 - 10,3,6,1,2,7,9 O/P: 22 [即 10+3+9]

Eg2) Array Elements - 7,1,2,3,6 O/P:19 Eg2) 数组元素 - 7,1,2,3,6 O/P:19

Eg3) Array Elements - 1,6,4,7,9 O/P:10 Eg3) 数组元素 - 1,6,4,7,9 O/P:10

Solution解决方案

outer: for (i = 0; i < elementsCount; ++i) {
            if (arr[i] == 6) {
                int sumBetweenBounds = arr[i];

                for (j = i + 1; j < elementsCount; ++j) {
                    if (arr[j] == 7) {
                        i = j;
                        continue outer;
                    }

                    sumBetweenBounds += arr[j];
                }

                sum += sumBetweenBounds;
                continue;
            }

            sum += arr[i];
        }

When talking about time complexity, we should differentiate between best-case, worst-case or average-case scenario ( https://en.wikipedia.org/wiki/Best,_worst_and_average_case ).在谈论时间复杂度时,我们应该区分最佳情况、最坏情况或平均情况( https://en.wikipedia.org/wiki/Best,_worst_and_average_case )。 When it is not mentioned, worst-case scenario is usually intended.当未提及时,通常会考虑最坏的情况。

In the worst-case scenario, your algorithm is O(n^2) because of the inner loop running when the array element is 6. In the worst-case, all array elements could be this value.在最坏的情况下,您的算法是 O(n^2),因为当数组元素为 6 时运行内部循环。在最坏的情况下,所有数组元素都可能是这个值。

In the best-case scenario, it is O(n), but this is usually not interesting.在最好的情况下,它是 O(n),但这通常并不有趣。

For average-case analysis, you would need to know the distribution of values in all arrays that your algorithm will be run on.对于平均情况分析,您需要知道将运行您的算法的所有数组中的值分布。

First, your algorithm wont work for an array of only 6's.首先,您的算法不适用于只有 6 个的数组。

Second, Your algorithm complexity is O(n^2), check the case of an array of only 6's.其次,你的算法复杂度是 O(n^2),检查只有 6 的数组的情况。

If there are only 6's you'll keep getting into the inner loop for every element which will give you the mentioned complexity and also keep summing up 6's that you already summed up before.如果只有 6 个,您将继续进入每个元素的内部循环,这将为您提供提到的复杂性,并继续总结您之前已经总结过的 6 个。

For example:例如:

int[] arr = new int[]{6,6,6,6};

will give:会给:

//1st iteration
sum += 6+6+6+6;
//2nd iteration
sum += 6+6+6;
//3rd
sum += 6+6;
//4th
sum += 6;

Which in total gives sum=60 instead of 24.总共给出sum=60而不是 24。

您使解决方案比实际问题更复杂,您应该使用单个 for 循环,并且您可以使用布尔值来检查是否要添加元素的天气

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

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