Explain the difference between O(1) vs O(n) space complexities
Explain the difference between O(1) vs O(n) space complexities
The difference between $$O(1)$$ and $$O(n)$$ space complexities lies in how the memory usage of an algorithm scales with the size of the input.
def swap(a, b):
temp = a
a = b
b = temp
a
, b
, and temp
), so its space complexity is $$O(1)$$[1][2][6].def create_list(n):
new_list = [0] * n
return new_list
n
is 10, the function creates a list of 10 elements. If n
is 100, it creates a list of 100 elements. Therefore, the memory usage grows linearly with the input size, making the space complexity $$O(n)$$[1][2][12].junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào