Published:
Updated:

  • 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