From eaec34147798a511733f3bd9d573aa189f15bea1 Mon Sep 17 00:00:00 2001 From: nikhylw <1.nikhil.wani+nikhly@gmail.com> Date: Sun, 17 May 2026 16:41:21 +0530 Subject: [PATCH] adding backtracking-2 soln --- W6_subsets.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 W6_subsets.py diff --git a/W6_subsets.py b/W6_subsets.py new file mode 100644 index 00000000..1ef397e4 --- /dev/null +++ b/W6_subsets.py @@ -0,0 +1,25 @@ +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + self.result = [] + + self.helper(nums, 0, []) + + return self.result + + def helper(self, nums, pivot, path): + + # Logic + self.result.append(list(path)) + + for i in range(pivot, len(nums)): + # Action + path.append(nums[i]) + # Recurse + self.helper(nums, i+1, path) + # Backtrack + path.pop() + + +# Time complexity: O(n * 2^n) +# Space complexity: O(n) +