Complex time complexity Erlang

I need help with the following:

flatten ([]) -> [];

flatten([H|T]) -> H ++ flatten(T).

The input list contains other lists with different lengths.

For instance:

flatten([[1,2,3],[4,7],[9,9,9,9,9,9]]).

What is the time complexity of this function? And why?

I got it in O (n), where n is the number of items in the input list.

For instance:

flatten([[1,2,3],[4,7],[9,9,9,9,9,9]])    n=3

flatten([[1,2,3],[4,7],[9,9,9,9,9,9],[3,2,4],[1,4,6]])    n=5

Thanks for the help.

+3
source share
1 answer

First of all, I'm not sure that your code will work, at least not in the way the standard library works. You can compare your function with lists:flatten/1and possibly improve your implementation. Try entering lists such as [a, [b, c]]and [[a], [b, [c]], [d]], and see if you returned the expected results.

- ++ () . Erlang ( ++), - ; , , - . , , ++, .

, A ++ B length(A), . length(FirstElement) + (lenght(FirstElement) + length(SecondElement)) + .... () , (n -1) * 1/2 * k * k, n - , k - . O(n^3).

, , . :

  • .
  • DO
  • Short ++
  • ++
+2

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


All Articles