Computing bigO runtime with two-dimensional values, where one dimension has an unknown length

I worked on collecting water between the towers and tried to figure out a large amount of my practice solution.

At some point, I create a 2D array of “towers” ​​from an array of user heights. At this stage, a nested loop cycle is used, where the inner loop runs heightmany times.

My BigO for this step then n * maxHeight?

I have never seen a single BigO view that used such a variable, but again I am pretty new, so this can be a problem.

I don’t feel that the height problem can be written off as constant, because there is no reason to believe that the height of the towers will not exceed the number of towers on a regular basis.

  //convert towerArray into 2D array representing towers
var multiTowerArray = [];
for (i = 0; i < towerArray.length; i++) {
  //towerArray is the user-input array of tower heights
    multiTowerArray.push([]);
    for (j = 0; j < towerArray[i]; j++) {
        multiTowerArray[i].push(1);
    }
}
+4
1

-, - - , . , O (n + U), n - , U - . , , O (nU), n - , U - .

, O (n + S), S - , , , .

, , . , ( n), ( m), , O (m + n log n) . , (, Aho-Corasick O (m + n + z), m n - , z - ). - , - O (& epsilon; - 1 log & delta; -1), & epsilon; & delta; , .

+2

Source: https://habr.com/ru/post/1652645/


All Articles