Grokking the Coding Interview: Patterns for Coding Questions
0% completed
Subsets (easy)
Problem Statement
Given a set with distinct elements, find all of its distinct subsets.
Example 1:
Input: [1, 3]
Output: [], [1], [3], [1,3]
Example 2:
Input: [1, 5, 3]
Output: [], [1], [5], [3], [1,5], [1,3], [5,3], [1,5,3]
Constraints:
1 <= nums.length <= 10-10 <= nums[i] <= 10- All the numbers of nums are
unique.
Try it yourself
Try solving this question here:
.....
.....
.....
Like the course? Get enrolled and start learning!
A
Anonymous
· 2 years ago
class Solution: def find_subsets(self, nums): subsets = [[]] temp = [] for num in nums: for subset in subsets: clone = subset[:] clone.append(num) temp.append(clone) for item in temp: subsets.append(item) temp = [] return subsets
A
Anonymous
· 4 years ago
Could I get clarification on the space complexity? Returned output list is O(n), and the subsets take up 2^n resulting in O(n*2^n)
So [1,2...] -> [[],[1],[2],[1,2]....]
Show 2 replies
A
Anonymous
· 5 years ago
I dint understand how's the time complexity of creating subsets is O(N*2^N). Can someone please help me understanding? Thanks!
Show 4 replies
Reading Progress
0%