【发布时间】:2021-07-18 17:48:30
【问题描述】:
我试图开发一种解决方案,以将 O(n^2) 或 O(n*m) 算法的时间复杂度降低到 O(n) 或 O(n+m) 算法。例如:
let arr = [[1, 2], [1, 2, 3], [1, 2, 3, 4, 5, 6, 7, 8]];
let x = 0;
let len = getArrayMaxLength (arr) //Get the maximum length of a 2d array which in this example is 8.
for (let i = 0; i < len && x < arr.length; ++i) {
print (arr [x][(i % arr [x].length);
if ((i + 1) % arr [x].length == 0) {
++x;
if (x != arr.length) i = -1;
}
}
我在确定这个算法的 Big-O 时遇到了问题,因为我从来没有处理过这么多条件的循环。我读过this 和this,但还是不太对劲。据我了解,时间复杂度将是 O(n+m)。其中 n 是 [arr.length],m 是 [len],它是上述函数 getArrayMaxLength 的输出。
所以总结一下。算法的时间复杂度是多少? 谢谢。
【问题讨论】:
标签: algorithm