• Reference
  • 파이썬 μ•Œκ³ λ¦¬μ¦˜ 인터뷰 μ±…μ—μ„œλŠ” _sum을 λΉΌμ£Όλ©΄μ„œ 0을 κΈ°μ€€μœΌλ‘œ λ°±νŠΈλž˜ν‚Ή ν•΄μ€Œ


Solution

from typing import List


class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        answer = []

        def dfs(index: int, _sum: int, elements: List[int]) -> None:
            if _sum > target:
                return

            # λ°±νŠΈλž˜ν‚Ή
            if _sum == target:
                answer.append(elements[:])
                return

            for i in range(index, len(candidates)):
                dfs(i, _sum + candidates[i], elements + [candidates[i]])

        dfs(0, 0, [])

        return answer

Leave a comment