Is the space complexity of this function N^2 as the output is a linked list?
I am learning about space complexity in school and am stuck on this question.
def myHealthcare(record):
l2=[]
count=0 # num of records generated and the specific time
for i in range(record):
l=[]
now = datetime.datetime.now()
ts = now.strftime("%d/%m/%Y %H:%M:%S") # str timestamp
ts=ts +' '+str(count)
l.append(ts)
l.append(rand.randint(36,39)) #temp
l.append(rand.randint(55,100)) #hr
l.append(rand.randint(55,100)) #Pulse
l.append(rand.randint(120,121)) #bp
l.append(rand.randint(11,17)) #respiratory rate
l.append(rand.randint(93,100)) #oxygen sat
l.append(round(rand.uniform(7.1,7.6),1)) #pH
l2.append(l)
count+=1
return l2
The space complexity of a linked list is not quadratic; each linked list node takes a constant amount of auxiliary memory, so the auxiliary memory used by the whole data structure is O(n) where n is the number of nodes.
However, you are also constructing strings and storing these in memory. The string str(count)
is part of a string that gets appended to your list l
on each iteration, and the length of this string is O(log n) since count
is a number up to n, and it has O(log n) digits when represented as a string. So the overall space complexity of this algorithm is O(n log n) because of that.