[LeetCode] Combination Sum

yoonene·2023년 2월 1일
0

알고리즘

목록 보기
44/62

문제 이동

첫 번째 제출

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        def combSum(index, res):
            if sum(res) == target:
                results.append(res)
                return
            elif sum(res) > target:
                return

            for i in range(index, len(candidates)):
                combSum(i, res + [candidates[i]])

        results = []
        combSum(0, [])
        return results

앞선 포스팅인 조합과 다른 점은 크게 없다.
1. 종료 조건이 합이 target이 되었을 때로 바뀐 것
2. 자기자신의 값까지 중복해서 사용 가능하게 하기 위해 인덱스를 i+1 대신 i로 한 것
밖에 차이가 없다.

코테 때도 이렇게 금방 풀어야 하는디ㅠ

profile
NLP Researcher / Information Retrieval / Search

0개의 댓글