Skip to content

Latest commit

 

History

History
76 lines (55 loc) · 2.11 KB

_78. Subsets.md

File metadata and controls

76 lines (55 loc) · 2.11 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 02, 2024

Last updated : July 01, 2024


Related Topics : Array, Backtracking, Bit Manipulation

Acceptance Rate : 79.81 %


Solutions

Python

# Minimizes the number of array copies formed to consistently hit 97% space efficiency
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        output = []
        def helper(currentList: List[int], remaining: List[int], add):
            if add :
                output.append(currentList.copy())
            
            if len(remaining) == 0 :
                return

            nextTerm = remaining.pop()
            helper(currentList, remaining, False)
            currentList.append(nextTerm)
            helper(currentList, remaining, True)
            remaining.append(currentList.pop())
            

        helper([], nums, True)

        return output
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        output = []
        def helper(currentList: List[int], remaining: List[int], add):
            if add :
                output.append(currentList)
            
            if len(remaining) == 0 :
                return

            nextTerm = remaining.pop()
            remainingCopy1 = remaining.copy()
            remainingCopy2 = remaining.copy()

            helper(currentList, remainingCopy1, False)
            newCurrentList = currentList.copy()
            newCurrentList.append(nextTerm)
            helper(newCurrentList, remainingCopy2, True)

        helper([], nums, True)

        return output