educative.io

Need for additional memory for storing sets

Do we need to have additional memory for storing the sets?

def find_subsets(nums):
  subsets = [[]]
  nums.sort()
  for index, num in enumerate(nums):
    start, end = 0, len(subsets)
    if index > 0 and num == nums[index - 1]:
      start = 1 << (index - 1)
    for ind in range(start, end):
      subsets.append(subsets[ind] + [num])

  return subsets

Hi @Shubh_Saxena!

You are right. We do not need to create and store the subset in the temporary variable set1. We can simply append it to the subsets list by replacing these lines:

set1 = list(subsets[j])
set1.append(nums[i])
subsets.append(set1)

with

subsets.append(subsets[j] + [nums[i])
1 Like