简体   繁体   中英

Simplifying O(nm) and O(n + m) time complexity

If I have a function that runs in O(nm) time complexity, is it common practice/acceptable to simplify that into just saying it runs in O(n^2) time complexity? For example, a function like this:

function funcOne(nArray, mArray) {
    for (var i = 0; i < nArray.length; i++) {
        for (var j = 0; j < mArray.length; j++) {

            ....

        }
    }
}

Also, if I have a function that runs in O(n + m) time complexity, is it common practice/acceptable to simplify that into just saying it runs in O(n) time complexity? For example, a function like this:

function funcOne(nArray, mArray) {
    for (var i = 0; i < nArray.length; i++) {

        ...

    }

    for (var j = 0; j < mArray.length; j++) {

        ...

    }
}

The sample you're provided is about O(N) complexity because the loops are executed sequentially. In case you put one loop into another it is O(N^2) complexity. What about n and m? For sure, if there is a relation between n and m, expressing N via n and m can be a tricky thing. For simplicity, you can think about N as an average of n and m, ie N = (n+m)/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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