The time complexity of this code to create a powerset from distinct integers is listed as O(n * 2^n) at all places including the Leetcode solution.
I listed the complexity of each step as a code comment and for me the overall complexity is coming out as O(n^2 * 2^n). You can see we are looping over n times on a line of code whose own complexity is O(n * 2^n), thus, shouldn't be the time complexity of this solution be O(n^2 * 2^n)?
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
n = len(nums)
output = [[]]
for num in nums: # O(n)
output += [curr + [num] for curr in output] # n for adding two lists and then 2^n times so O(n * 2^n)
return output
n_0andcsuch that for alln > n_0,n * 2^n < c * 2^n. Informally, you can discard lower order terms but not lower order factors.O(2^n)andO(n^2 * 2^n)is relevant for a given purpose, they are different - unlike, say,O(2^n)andO(n^2 + 2^n), which are the same.n *factor will ever matter, but yeah, formally, it can't be dropped. (I'd leave the comments, but I think this is distracting from the OP's problem, not helping)