[英]Is it linear Time Complexity of Bigger?
Can any one tell me what is the worst time complexity of below code?谁能告诉我以下代码的最差时间复杂度是多少? Is it linear or bigger?它是线性的还是更大的?
void fun(int[] nums){
{
int min = min(nums);
int max = max(nums);
for(int i= min; i<=max;i++){
print(i); //constant complexity for print
}
}
int min(int[] nums);//return min in nums in linear time
int max(int[] nums);//return max in nums in linear time
where 0 <= nums.length <= 10^4 and -10^9 <= nums[i] <= 10^9其中 0 <= nums.length <= 10^4 和 -10^9 <= nums[i] <= 10^9
Can I say that time complexity of this code is O(Max(nums[i]) - Min(nums[i])) and can I say, this is linear time complexity?我可以说这段代码的时间复杂度是O(Max(nums[i]) - Min(nums[i]))我可以说这是线性时间复杂度吗?
As the complexity is linear with respect to the range R = max - min
of the data, I would call it a pseudo-linear complexity.由于复杂性相对于数据的R = max - min
范围是线性的,因此我将其称为伪线性复杂性。 O(N + R). O(N + R)。
This is detailed in this Wikipedia entry: Pseudo-polynomial time这在此 Wikipedia 条目中有详细说明:伪多项式时间
As mentioned in the introduction of this article:正如这篇文章的介绍所提到的:
In computational complexity theory, a numeric algorithm runs in pseudo-polynomial time if its running time is a polynomial in the numeric value of the input (the largest integer present in the input)—but not necessarily in the length of the input (the number of bits required to represent it), which is the case for polynomial time algorithms.在计算复杂性理论中,如果数值算法的运行时间是输入数值的多项式(输入中存在的最大 integer),则数值算法在伪多项式时间内运行,但不一定在输入的长度(数字表示它所需的位数),这是多项式时间算法的情况。
Generally, when analysing the complexity of a given algorithm, we don't make any specific assumption about the inherent range limitation of a particular targeted language, except of course if this is especially mentionned in the problem.通常,在分析给定算法的复杂性时,我们不会对特定目标语言的固有范围限制做出任何具体假设,当然,除非问题中特别提到了这一点。
If the range of numbers is constant (ie -10^9 <= nums[i] <= 10^9
) then如果数字范围是恒定的(即-10^9 <= nums[i] <= 10^9
),那么
for(int i= min; i<=max;i++){
print(i); //constant complexity for print
}
is in O(1) , ie constant because you know, it iterates at most 2 * 10^9
numbers, regardless of how many numbers there are in the nums[]
array.在O(1)中,即常量,因为您知道,它最多迭代2 * 10^9
数字,无论nums[]
数组中有多少个数字。 Thus it does not depend on the size of the input array.因此它不依赖于输入数组的大小。
Consider the following input arrays考虑以下输入 arrays
nums = [-10^9, 10^9]; //size 2
nums = [-10^9, -10^9 + 1, -10^9 + 2, ..., 10^9 - 2, 10^9 - 1, 10^9] //size 2 * 10^9 + 1
for both min
and max
will have the same values -10^9
and 10^9
respectively. min
和max
将分别具有相同的值-10^9
和10^9
。 Thus your loop will iterate all numbers from -10^9
to 10^9
.因此,您的循环将迭代从-10^9
到10^9
的所有数字。 And even if there were 10^100000 numbers in the orginal array, the for
loop will at most iterate from -10^9
to 10^9
.即使原始数组中有 10^100000 个数字, for
循环也最多会从-10^9
迭代到10^9
。
And you say min()
and max()
are in O(n) , thus your overall algorithm would also be in O(n) .你说min()
和max()
在O(n)中,因此你的整体算法也将在O(n)中。 But if you then take into account that the given maximum length (10^4) of the array is by magnitudes smaller then the limit of your numbers, you can even neglect calling min
and max
但是,如果您考虑到数组的给定最大长度(10 ^ 4)比您的数字限制小很多,您甚至可以忽略调用min
和max
And as for your comment至于你的评论
For ex.例如。 array =[1,200,2,6,4,100].数组 =[1,200,2,6,4,100]。 In this case we can find min and max in linear time(O(n) where n is length of array).在这种情况下,我们可以在线性时间内找到最小值和最大值(O(n),其中 n 是数组的长度)。 Now, my for loop complexity is O(200) or O(n^3) which is much more than length of array.现在,我的 for 循环复杂度是 O(200) 或 O(n^3),这远远超过数组的长度。 Can I still say its linear complexity我还能说它的线性复杂度吗
The size of the array and the values in the array are completely independent of each other.数组的大小和数组中的值完全相互独立。 Thus you cannot express the complexity of the for
loop in terms of n
(as explained above).因此,您不能用n
来表达for
循环的复杂性(如上所述)。 If you really want to take into account also the range of the numbers, you have to express it somehow like this O(n + r) where n is the size of the array, and r is the range of the numbers.如果你真的想考虑数字的范围,你必须以某种方式表达它O(n + r)其中n是数组的大小, r是数字的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.