简体   繁体   中英

How do you calculate this space complexity of this algorithm?

I am working on space complexity of algorithms. There is an algorithm that can compute the max for each pair in an array until the list length become 1. Given the array is [25,22,27,33].

[25] [22] [27] [33] 
  [25] [27] [33]
    [27] [33]
       [33]

How do you calculate the space complexity of this algorithm?

It would be fine, if you can provide a code, than we can calculate time and space complexity.

First thing, do you dropout lowest element of array or some random element? If you drop out lowest element in each iteration, it is a simple problem. In O(n) time complexity you can find a pair of element with highest sum. Actually you find two number with biggest value, and store them as highest pair in all iteration, after that in all iteration you decrease number of your element, and need O(n) time to rearrange your array,and you need to do that n-1 times . Upper limit is O(n^2). Otherwise, if you drop out random number, structure of algorithms is same but you need find two maximum number in current array.

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