LeetCode Combination Sum (JavaScript), BackTracking

2025. 11. 25. 18:01·코딩테스트/LeetCode

링크 : https://leetcode.com/problems/combination-sum/description/

문제 설명

더보기

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Example 1:

Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.

Example 2:

Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]

Example 3:

Input: candidates = [2], target = 1
Output: []

Constraints:

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • All elements of candidates are distinct.
  • 1 <= target <= 40

문제 풀이

백트래킹(dfs)을 이용하여 문제 풀이

1. dfs param : index (탐색할 위치), sum (총 합), selected (선택된 값)

2. sum === target 이라면 answer에 selected을 복사하여 push

  • 복사하여 push 하는 이유 : array의 경우 call by reference로 참조 값을 push하게 됨.
  • 즉, 이후 push 했던 selected가 변하면 answer의 selected도 변함 -> 따라서 참조 값이 달라지도록 새로 만들어 push

3. 반복문 로직

  • 시작지점은 같은 숫자를 사용할 수 있으므로 dfs에 현재 index부터 검사 시작 (자기자신을 다시 탐색할 수 있도록)
  • 또한, sum + candidates[i] 가 target보다 클 경우 탐색할 필요가 없으므로 탐색을 시작하지 않음 (백트래킹)
아래 코드에서 if 문으로 처리를 했지만 백트래킹을 좀 더 쉽게 파악하려면
if (sum + candidates[i] <= target) 대신
dfs함수 내부에 if (sum > target) return; 을 추가 해주면 됨
즉, 더이상 진행할 필요가 없다면 종료하도록 (백트래킹)
/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
var combinationSum = function(candidates, target) {
    const answer = [];

    const dfs = (index, sum, selected) => {
        if (sum === target) {
            answer.push([...selected]);
            return;
        }

       for (let i = index; i < candidates.length; i++) {
            if (sum + candidates[i] <= target) {
                selected.push(candidates[i]);
                dfs(i, sum + candidates[i], selected);
                selected.pop(candidates[i]);
            }
       }
    }

    dfs(0, 0, []);

    return answer;
};

결론

시작하는 지점 (자기 자신을 다시 선택할 수 있으므로 index설정 주의)과 백트래킹을 이용하여 유요한 범위내에서 검사할 수 있도록 지정

추가로 최종적으로 답을 push할 땐 복사하여 같은 참조 값을 넣지 않도록 주의

'코딩테스트 > LeetCode' 카테고리의 다른 글

LeetCode Multiply Strings (JavaScript)  (0) 2025.11.25
LeetCode Combination Sum || (JavaScript), BackTracking  (0) 2025.11.25
LeetCode Count and Say (JavaScript)  (0) 2025.11.25
LeetCode Valid Sudoku (JavaScript), Set  (0) 2025.11.24
LeetCode Find First and Last Position of Element in Sorted Array (JavaScript), 이분 탐색  (1) 2025.11.24
'코딩테스트/LeetCode' 카테고리의 다른 글
  • LeetCode Multiply Strings (JavaScript)
  • LeetCode Combination Sum || (JavaScript), BackTracking
  • LeetCode Count and Say (JavaScript)
  • LeetCode Valid Sudoku (JavaScript), Set
의현
의현
개발하는 하루
  • 의현
    UIHYEON
    의현
  • 링크

    • 김의현 포트폴리오
    • GitHub
    • LinkedIn
  • 전체
    오늘
    어제
    • 분류 전체보기 (213) N
      • 프론트엔드 (64)
        • JavaScript (51)
        • HTML (3)
        • React (7)
        • CSS (2)
        • CS (1)
      • 프로젝트 (19)
        • Portfolio 사이트 개발 (19)
      • 코딩테스트 (127) N
        • Binary Search (2)
        • bfs (Breadth-first s.. (4)
        • dfs (Deapth-first se.. (1)
        • Greedy (1)
        • Dynamic Programming (1)
        • two pointer (4)
        • 구현 (2)
        • LIS(Longest Increasi.. (0)
        • 문자열 (3)
        • 자료구조 (6)
        • 비트마스크 (2)
        • 수학 (2)
        • 프로그래머스 (61)
        • LeetCode (38) N
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
의현
LeetCode Combination Sum (JavaScript), BackTracking
상단으로

티스토리툴바